android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單
實(shí)現(xiàn)此功能沒有太多的技術(shù)難點(diǎn),主要通過PopupWindow方法,同時(shí)更進(jìn)一步加深了PopupWindow的使用,實(shí)現(xiàn)點(diǎn)擊彈出一個(gè)自定義的view,view里面可以自由設(shè)計(jì),比較常用的可以放一個(gè)listview。
demo中我只是一個(gè)點(diǎn)擊展示,簡(jiǎn)單的使用了fade in out的動(dòng)畫效果,也沒有精美的圖片資源,看著也丑,不過這么短的時(shí)間,讓你掌握一個(gè)很好用的技術(shù),可以自己擴(kuò)展,不很好么?
廢話不說了,直接上代碼:
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
private PopupWindow popupwindow;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (popupwindow != null&&popupwindow.isShowing()) {
popupwindow.dismiss();
return;
} else {
initmPopupWindowView();
popupwindow.showAsDropDown(v, 0, 5);
}
break;
default:
break;
}
}
public void initmPopupWindowView() {
// // 獲取自定義布局文件pop.xml的視圖
View customView = getLayoutInflater().inflate(R.layout.popview_item,
null, false);
// 創(chuàng)建PopupWindow實(shí)例,200,150分別是寬度和高度
popupwindow = new PopupWindow(customView, 250, 280);
// 設(shè)置動(dòng)畫效果 [R.style.AnimationFade 是自己事先定義好的]
popupwindow.setAnimationStyle(R.style.AnimationFade);
// 自定義view添加觸摸事件
customView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
popupwindow = null;
}
return false;
}
});
/** 在這里可以實(shí)現(xiàn)自定義視圖的功能 */
Button btton2 = (Button) customView.findViewById(R.id.button2);
Button btton3 = (Button) customView.findViewById(R.id.button3);
Button btton4 = (Button) customView.findViewById(R.id.button4);
btton2.setOnClickListener(this);
btton3.setOnClickListener(this);
btton4.setOnClickListener(this);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:background="#C0C0C0"
android:text="點(diǎn)擊下拉列表" />
</RelativeLayout>
自定義view的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0" >
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="70dp"
android:text="viviens" />
<Button
android:id="@+id/button3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:paddingRight="70dp"
android:text="mryang" />
<Button
android:id="@+id/button4"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button3"
android:paddingRight="70dp"
android:text="張曉達(dá)" />
</RelativeLayout>
動(dòng)畫效果:
inputodown.xml 進(jìn)入屏幕
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="-100%"
android:toYDelta="0" />
</set>
outdowntoup.xml
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="-100%" />
</set>
styles.xml
<style name="AnimationFade"> <!-- PopupWindow左右彈出的效果 --> <item name="android:windowEnterAnimation">@anim/inuptodown</item> <item name="android:windowExitAnimation">@anim/outdowntoup</item> </style>
實(shí)現(xiàn)效果:

以上所述就是本文對(duì)android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單的全部?jī)?nèi)容,希望大家喜歡。
- Android中實(shí)現(xiàn)毛玻璃效果的3種方法
- Android 實(shí)現(xiàn)圖片模糊、高斯模糊、毛玻璃效果的三種方法
- Android模糊處理實(shí)現(xiàn)圖片毛玻璃效果
- Android模糊處理簡(jiǎn)單實(shí)現(xiàn)毛玻璃效果
- Android毛玻璃背景效果簡(jiǎn)單實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)圖片毛玻璃背景效果
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
- Android ListView長(zhǎng)按彈出菜單二種實(shí)現(xiàn)方式示例
- Android實(shí)現(xiàn)毛玻璃效果彈出菜單動(dòng)畫
相關(guān)文章
Matrix的set,pre,post調(diào)用順序詳解
下面小編就為大家?guī)硪黄狹atrix的set,pre,post調(diào)用順序詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android長(zhǎng)按imageview把圖片保存到本地的實(shí)例代碼
本文通過代碼給大家介紹了Android長(zhǎng)按imageview把圖片保存到本地的實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-12-12
Android側(cè)滑導(dǎo)航欄的實(shí)例代碼
這篇文章主要介紹了Android側(cè)滑導(dǎo)航欄的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android拖拽助手ViewDragHelper的創(chuàng)建與使用實(shí)例
ViewDragHelper是針對(duì) ViewGroup 中的拖拽和重新定位 views 操作時(shí)提供了一系列非常有用的方法和狀態(tài)追蹤,下面這篇文章主要給大家介紹了關(guān)于Android拖拽助手ViewDragHelper的創(chuàng)建與使用的相關(guān)資料,需要的朋友可以參考下2022-05-05
TabLayout+ViewPager2的簡(jiǎn)單使用詳解
這篇文章主要為大家詳細(xì)介紹了TabLayout+ViewPager2的簡(jiǎn)單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android Studio Gradle插件版本與Gradle版本之間的對(duì)應(yīng)關(guān)系
今天小編就為大家分享一篇關(guān)于Android Studio Gradle插件版本與Gradle版本之間的對(duì)應(yīng)關(guān)系,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘
這篇文章主要為大家詳細(xì)介紹了Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android實(shí)現(xiàn)單頁面浮層可拖動(dòng)view的一種方法
本篇文章主要介紹了Android實(shí)現(xiàn)單頁面浮層可拖動(dòng)view的一種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Android Handler 機(jī)制實(shí)現(xiàn)原理分析
本文主要介紹 Android Handle機(jī)制實(shí)現(xiàn)的原理,這里整理了詳細(xì)的關(guān)于Handler的資料以及工作流程和實(shí)際應(yīng)用,有興趣的小伙伴可以參考下2016-08-08

