項目回顧-PopupWindow

右上菜單,可以通過 重寫?onCreateOptionsMenu指定 menu, 重寫?onOptionsItemSelected 來響應點擊事件

不過 這個菜單在某些手機上彈出的有點卡頓,而且如果不對主題進行設置,會從actionbar 上直接彈出,而不是下面

如果想從下面彈出,要先添加一個style

    <style name="MenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu.Overflow"><item name="overlapAnchor">false</item></style>

之后在activity引用的主題中添加一行 ?就行了

 <item name="actionOverflowMenuStyle">@style/MenuStyle</item>

?

Menu在沒有特別要求的情況還是很好用的,但是如果要求比較復雜的時候就不如用PopupWindow了

要實現如圖的效果

?

?先拿到從美工拿到的圖片 ?

?

打開sdk的 tools文件夾 ? 打開?draw9patch.bat ?把圖片拖進去

畫黑邊 ? 左上兩個方向的黑邊 表示拉伸部分

右下表示布局中控件內容顯示的區域

如果畫錯了 按住shift 抹掉

完成之后save

?

?

布局

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3               android:layout_width="wrap_content"
  4               android:layout_height="wrap_content"
  5               android:background="@drawable/pop_bg_shadow9"
  6               android:gravity="center_horizontal"
  7               android:orientation="vertical"
  8     >
  9 
 10     <LinearLayout
 11         android:id="@+id/ll_action_config"
 12         android:layout_width="match_parent"
 13         android:layout_height="wrap_content"
 14         android:gravity="center_vertical"
 15         android:orientation="horizontal"
 16         android:padding="10dp"
 17         >
 18 
 19         <ImageView
 20             android:layout_width="25dp"
 21             android:layout_height="25dp"
 22             android:src="@drawable/pop_setting"/>
 23 
 24         <TextView
 25             android:layout_width="wrap_content"
 26             android:layout_height="wrap_content"
 27             android:paddingLeft="15dp"
 28             android:text="關聯配置"
 29             android:textColor="@color/white"/>
 30 
 31     </LinearLayout>
 32 
 33     <LinearLayout
 34         android:id="@+id/ll_action_history"
 35         android:layout_width="match_parent"
 36         android:layout_height="wrap_content"
 37         android:gravity="center_vertical"
 38         android:orientation="horizontal"
 39         android:padding="10dp"
 40         >
 41 
 42         <ImageView
 43             android:layout_width="25dp"
 44             android:layout_height="25dp"
 45             android:src="@drawable/pop_history"/>
 46 
 47         <TextView
 48             android:layout_width="wrap_content"
 49             android:layout_height="wrap_content"
 50             android:paddingLeft="15dp"
 51             android:text="歷史記錄"
 52             android:textColor="@color/white"/>
 53 
 54     </LinearLayout>
 55 
 56     <LinearLayout
 57         android:id="@+id/ll_action_ip"
 58         android:layout_width="match_parent"
 59         android:layout_height="wrap_content"
 60         android:gravity="center_vertical"
 61         android:orientation="horizontal"
 62         android:padding="10dp"
 63         >
 64 
 65         <ImageView
 66             android:layout_width="25dp"
 67             android:layout_height="25dp"
 68             android:src="@drawable/pop_ip"/>
 69 
 70         <TextView
 71             android:layout_width="wrap_content"
 72             android:layout_height="wrap_content"
 73             android:paddingLeft="15dp"
 74             android:text="IP地址"
 75             android:textColor="@color/white"/>
 76 
 77     </LinearLayout>
 78 
 79     <LinearLayout
 80         android:id="@+id/ll_action_about"
 81         android:layout_width="match_parent"
 82         android:layout_height="wrap_content"
 83         android:gravity="center_vertical"
 84         android:orientation="horizontal"
 85         android:padding="10dp"
 86         >
 87 
 88         <ImageView
 89             android:layout_width="25dp"
 90             android:layout_height="25dp"
 91             android:src="@drawable/pop_about"/>
 92 
 93         <TextView
 94             android:layout_width="wrap_content"
 95             android:layout_height="wrap_content"
 96             android:paddingLeft="15dp"
 97             android:text="關于"
 98             android:textColor="@color/white"/>
 99 
100     </LinearLayout>
101 </LinearLayout>
View Code

?

現在要實現點擊 右上菜單 彈出 這個布局,同時菜單的圖標會變換。

?

在menu目錄 新建一個xml ? ?默認icon是?more_close

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/action_menu"android:icon="@drawable/more_close"android:title="菜單"app:showAsAction="always"></item>
</menu>

?

回到activity

    @Overridepublic boolean onCreateOptionsMenu(Menu menu) {this.menu = menu;getMenuInflater().inflate(R.menu.face, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.action_menu:View view=findViewById(R.id.action_menu);showActionMenuPopup(view);break;}return super.onOptionsItemSelected(item);}

?

紅色部分都是關鍵部分

先看 ?showActionMenuPopup ?這個彈出菜單的方法?

?

首先要改變菜單的圖標

menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.more_open));

這個menu是 全局的

private Menu menu;

在?onCreateOptionsMenu 中 把這個menu初始化

?

之后 設置?PopupWindow?

 1         View view=LayoutInflater.from(this).inflate(R.layout.popup_actionmenu,null);
 2         actionmenupopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
 3         actionmenupopupWindow.setContentView(view);
 4 
 5         view.findViewById(R.id.ll_action_config).setOnClickListener(this);
 6         view.findViewById(R.id.ll_action_history).setOnClickListener(this);
 7         view.findViewById(R.id.ll_action_ip).setOnClickListener(this);
 8         view.findViewById(R.id.ll_action_about).setOnClickListener(this);
 9 
10         actionmenupopupWindow.setFocusable(true);
11         actionmenupopupWindow.setBackgroundDrawable(new BitmapDrawable());
12 
13         actionmenupopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
14             @Override
15             public void onDismiss() {
16                 menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.more_close));
17             }
18         });
19 
20         actionmenupopupWindow.showAsDropDown(v);

?

5-8 ?設置彈出菜單的點擊事件

11 ?點擊PopupWindow之外的地方PopupWindow消失

13-18 ?監聽PopupWindow消失 ?改變菜單圖標

20 ?showAsDropDown ?從下方出現 ? 肯定要是一個view的, 回到onOptionsItemSelected ?找到右上菜單的view ?傳入了方法中,最后在這里使用

?

還有一個顯示方法 ? 直接指定位置,不過要測量出狀態欄和actionbar的高度

actionmenupopupWindow.showAtLocation(findViewById(R.id.action_menu),Gravity.RIGHT|Gravity.TOP ,0, getStatusBarHeight(this)+getSupportActionBar().getHeight());

?

?

IOS有一個底部菜單控件?UIActionSheet

安卓方面想實現這個 ?有第三方的 ActionSheet? ? 有 谷歌官方的design包 ?BottomSheet

用PopupWindow 也可以實現

?

rootview = LayoutInflater.from(this).inflate(R.layout.activity_main,null);

?

 1         View view= LayoutInflater.from(this).inflate(R.layout.popup_camera_sel,null);
 2         bottompopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
 3 
 4         bottompopupWindow.setContentView(view);
 5 
 6         view.findViewById(R.id.ll_gallery).setOnClickListener(this);
 7         view.findViewById(R.id.ll_camera).setOnClickListener(this);
 8         view.findViewById(R.id.ll_cancle).setOnClickListener(this);
 9 
10         WindowManager.LayoutParams lp = getWindow().getAttributes();
11         lp.alpha = 0.6f;
12         getWindow().setAttributes(lp);
13 
14         bottompopupWindow.setFocusable(true);
15         bottompopupWindow.setBackgroundDrawable(new BitmapDrawable());
16         bottompopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
17 
18         bottompopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
19             @Override
20             public void onDismiss() {
21                 WindowManager.LayoutParams lp = getWindow().getAttributes();
22                 lp.alpha = 1f;
23                 getWindow().setAttributes(lp);
24             }
25         });
26         bottompopupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);

?

10-12 ? 18-25 ? 改變屏幕的透明度

?

style ? ? ?從底部升上來 降下去的動畫?

    <style name="mypopwindow_anim_style"><item name="android:windowEnterAnimation">@anim/popshow_anim</item><!-- 指定顯示的動畫xml --><item name="android:windowExitAnimation">@anim/pophidden_anim</item><!-- 指定消失的動畫xml --></style>

show

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="500"android:fromYDelta="50%p"android:toYDelta="0" />
</set>

hide

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="500"android:fromYDelta="0"android:toYDelta="50%p" />
</set>

?

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"android:orientation="vertical"android:gravity="center"><LinearLayoutandroid:id="@+id/ll_gallery"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="相冊"android:textColor="#333333"android:textSize="20sp"/></LinearLayout><!--<TextView--><!--android:layout_width="200dp"--><!--android:layout_height="1dp"--><!--android:background="#838B8B"--><!--/>--><ImageViewandroid:src="@drawable/line_pop"android:scaleType="centerCrop"android:layout_width="match_parent"android:layout_height="wrap_content"/><LinearLayoutandroid:id="@+id/ll_camera"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="相機"android:textColor="#333333"android:textSize="20sp"/></LinearLayout><!--<TextView--><!--android:layout_width="200dp"--><!--android:layout_height="1dp"--><!--android:background="#838B8B"--><!--/>--><ImageViewandroid:src="@drawable/line_pop"android:scaleType="centerCrop"android:layout_width="match_parent"android:layout_height="wrap_content"/><LinearLayoutandroid:id="@+id/ll_cancle"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="取消"android:textSize="20sp"/></LinearLayout></LinearLayout>
View Code

?

?

?

?

最后 記得響應完點擊事件之后 ?dismiss 關閉PopupWindow

?

轉載于:https://www.cnblogs.com/demon9/p/6021980.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/456988.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456988.shtml
英文地址,請注明出處:http://en.pswp.cn/news/456988.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

android listpreference 自定義,Android ListPreference的用法一

xmlns:android"http://schemas.android.com/apk/res/android"android:key"screen_list"android:title"標題"android:summary"說明摘要">< ListPreferenceandroid:key"myListPreference"android:title"標題"…

C語言求最大公約數和最小公倍數的幾種算法

求最小公倍數算法&#xff1a; 最小公倍數兩整數的乘積最大公約數 求最大公約數算法&#xff1a; (1)輾轉相除法 有兩整數a和b&#xff1a; ① a%b得余數c ② 若c0&#xff0c;則b即為兩數的最大公約數 ③ 若c≠0&#xff0c;則ab&#xff0c;bc&#xff0c;再回去執行①…

3月15日云棲精選夜讀:雙管齊下,MaxCompute數據上云與生態

雙管齊下&#xff0c;MaxCompute數據上云與生態 作者&#xff1a;場景研讀 Go語言并發機制初探 作者&#xff1a;邴越 趣拍云短視頻SDK全面升級&#xff0c;簡單易用引開發者點贊 作者&#xff1a;sherry是雪梨 發表在&#xff1a;趣拍云團隊 阿里云機器學習平臺編程模型演…

qt android glsl,基于Qt的OpenGL學習(1)—— Hello Triangle

簡介要學習OpenGL的話&#xff0c;強烈安利這個教程JoeyDeVries的learnopengl&#xff0c;這里是中文翻譯好的版本。教程中使用OpenGL是通過GLFW這個庫&#xff0c;而在Qt中對OpenGL封裝得很好&#xff0c;并且和GUI以及IO相關的處理Qt更便捷&#xff0c;學習起來更輕松。這里就…

解決:Not Found: /favicon.ico

直接說解決辦法&#xff1a; &#xff08;1&#xff09;制作一個 favicon.ico圖標放在<head></head>標簽中 <link rel"shortcut icon" href"xxxxxxxxxx.ico" type"image/x-icon" /> <!--制作的圖標&#xff0c;使用hr…

多態方法調用的解析和分派

方法調用并不等同于方法執行&#xff0c;方法調用階段唯一的任務就是確定被調用方法的版本&#xff08;即調用哪一個方法&#xff09;&#xff0c;暫時還不涉及方法內部的具體運行過程。在程序運行時&#xff0c;進行方法調用是最普遍、最頻繁的操作&#xff0c;Class文件的編譯…

ES6:Set和Map

Set Set:類似數組&#xff0c;但是成員的值都是唯一的&#xff0c;沒有重復。Set本身是一個構造函數&#xff0c;用來生成Set數據結構。他包含的方法&#xff1a;add: 添加某個值&#xff0c;返回Set結構本身。delete: 刪除某個值&#xff0c;返回一個布爾值&#xff0c;表示是…

九九乘法表[循環嵌套]

#九九乘法表 # 1*11 # 1*22 2*24 # 1*33 2*36 3*39 # ...#循環嵌套 #行數 i 1 while i < 9:# 打印每行的內容j 1while j < i:print("%d * %d %3d " % (i, j, i * j), end)j 1print() # 換行i 1while嵌套&#xff1a;w 1 while w < 10: #外層循…

關于用VS寫C程序運行時出現燙字以及亂碼的問題的原因

最近在復習C語言寫程序時&#xff0c;突然碰到標題上的這種情況&#xff0c;后來經過上網查找以及逐步調試才發現原來是在打印數組的時候“越界”導致的&#xff0c;因為程序在默認初始化char類型的數組時&#xff0c;初始化的值是“燙”字&#xff0c;一般情況下是字符串未初始…

javascript函數調用的各種方法!!

在JavaScript中一共有下面4種調用方式&#xff1a; (1) 基本函數調用 (2)方法調用 (3)構造器調用 (4)通過call()和apply()進行調用 1. 基本函數調用 普通函數調用模式&#xff0c;如&#xff1a; JavaScript code?1234function fn(o){…… }fn({x:1});在基本函數調用中&#x…

ARM TK1 安裝kinect驅動

首先安裝usb庫 $ git clone https://github.com/libusb/libusb.git 編譯libusb需要的工具 $ sudo apt-get install autoconf autogen $ sudo apt-get install libtool $ sudo apt-get install libudev* 編譯安裝 $ sudo ./autogen.sh $ sudo make $ sudo make install $ sudo l…

如何在一個html頁面中提交兩個post,如何在同一個頁面上從Django和Ajax獲得多個post請求?...

我一整天都在為這事犯愁。似乎什么都沒用。這是我的情況。在我有一個Django表單&#xff0c;有兩個字段&#xff1a;redirect_from&#xff0c;redirect_to。此表單有兩個提交按鈕&#xff1a;Validate和{}。當頁面加載時&#xff0c;Submit被隱藏&#xff0c;只顯示Validate。…

大數據入門:各種大數據技術的介紹

大數據我們都知道hadoop&#xff0c;可是還會各種各樣的技術進入我們的視野&#xff1a;Spark&#xff0c;Storm&#xff0c;impala&#xff0c;讓我們都反映不過來。為了能夠更好的架構大數據項目&#xff0c;這里整理一下&#xff0c;供技術人員&#xff0c;項目經理&#xf…

高可用與負載均衡(5)之基于客戶端的負載均衡

什么是客戶端負載均衡 基于客戶端的負載均衡&#xff0c;簡單的說就是在客戶端程序里面&#xff0c;自己設定一個調度算法&#xff0c;在向服務器發起請求的時候&#xff0c;先執行調度算法計算出向哪臺服務器發起請求&#xff0c;然后再發起請求給服務器。 基于客戶端負載均衡…

Variant 與 內存泄露

http://blog.chinaunix.net/uid-10386087-id-2959221.html 今天遇到一個內存泄露的問題。是師兄檢測出來的。Variant類型在使用后要Clear否則會造成內存泄露&#xff0c;為什么呢&#xff1f; Google一下找到下面一篇文章&#xff0c;主要介紹了Com的內存泄露&#xff0c;中間有…

安裝安全類軟件進行了android簽名漏洞修補,魅族MX3怎么升級固件體驗最新比較穩定的版本...

魅族mx3固件怎么升級?flyme os系統會持續更新&#xff0c;升級魅族MX3手機系統需先下載MX3的升級固件&#xff0c;升級固件分為體驗版和穩定版。魅族MX3固件有體驗版和穩定版兩種&#xff0c;顧名思義&#xff0c;體驗版為最新版但相比穩定版來說存在更多的漏洞&#xff0c;升…

linux su切換用戶提示Authentication failture的解決辦法

由于ubtun系統默認是沒有激活root用戶的&#xff0c;需要我們手工進行操作&#xff0c;在命令行界面下&#xff0c;或者在終端中輸入如下命令&#xff1a; sudo passwd Password&#xff1a;你當前的密碼 Enter new UNIX password&#xff1a;這個是root的密碼 Retype new …

@property

class Person(object):def __init__(self, name,age):#屬性直接對外暴露#self.age age#限制訪問self.__age ageself.__name namedef getAge(self):return self.__agedef setAge(self,age):if age<0:age 0self.__age age#方法名為受限制的變量去掉雙下劃線propertydef a…

ubuntu入門知識

1、linux系統發展歷史 unix -> Linux -> ubuntu linux發展軌跡圖 2、ubuntu下載和安裝 推薦使用長期支持版本&#xff1a; 10.04,12.04,14.04或LTS版本 安裝環境VMware虛擬機 3、安裝之后創建root sudo passwd root 輸入root用戶密碼即可 4、安裝軟件&#xff1a; 更新軟…

html 二級試題,計算機二級考試WEB試題及答案

計算機二級考試WEB試題及答案當前主要的 WEB數據庫訪問技術有哪些?答&#xff1a;到目前為止&#xff0c;WEB數據庫訪問技術主要分為兩大類&#xff1a;(1)公共網關接口技術(CGI);CGI 是 WEB 服務器運行時外部程序的規范&#xff0c;按照 CGI 編寫的程序可以擴展服務器的功能&…