android使用PopupWindow實現(xiàn)頁面點擊頂部彈出下拉菜單
實現(xiàn)此功能沒有太多的技術難點,主要通過PopupWindow方法,同時更進一步加深了PopupWindow的使用,實現(xiàn)點擊彈出一個自定義的view,view里面可以自由設計,比較常用的可以放一個listview。
demo中我只是一個點擊展示,簡單的使用了fade in out的動畫效果,也沒有精美的圖片資源,看著也丑,不過這么短的時間,讓你掌握一個很好用的技術,可以自己擴展,不很好么?
廢話不說了,直接上代碼:
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實例,200,150分別是寬度和高度
popupwindow = new PopupWindow(customView, 250, 280);
// 設置動畫效果 [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;
}
});
/** 在這里可以實現(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="點擊下拉列表" />
</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="張曉達" />
</RelativeLayout>
動畫效果:
inputodown.xml 進入屏幕
<?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>
實現(xiàn)效果:

以上所述就是本文對android使用PopupWindow實現(xiàn)頁面點擊頂部彈出下拉菜單的全部內(nèi)容,希望大家喜歡。
- Android中實現(xiàn)毛玻璃效果的3種方法
- Android 實現(xiàn)圖片模糊、高斯模糊、毛玻璃效果的三種方法
- Android模糊處理實現(xiàn)圖片毛玻璃效果
- Android模糊處理簡單實現(xiàn)毛玻璃效果
- Android毛玻璃背景效果簡單實現(xiàn)代碼
- Android實現(xiàn)圖片毛玻璃背景效果
- android popwindow實現(xiàn)左側彈出菜單層及PopupWindow主要方法介紹
- 基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出
- Android ListView長按彈出菜單二種實現(xiàn)方式示例
- Android實現(xiàn)毛玻璃效果彈出菜單動畫
相關文章
Android studio git創(chuàng)建與刪除標簽(Tag)的教程詳解
這篇文章主要介紹了Android studio git創(chuàng)建與刪除標簽(Tag)的教程詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
ListView實現(xiàn)頂部和底部內(nèi)容指示器的方法
這篇文章主要介紹了ListView實現(xiàn)頂部和底部內(nèi)容指示器的方法,需要的朋友可以參考下2015-09-09
Flutter實現(xiàn)軟鍵盤與其它區(qū)域絲滑切換效果
這篇文章主要為大家詳細介紹了如何使用Flutter實現(xiàn)軟鍵盤與其它區(qū)域絲滑切換效果,文中的示例代碼講解詳細,需要的小伙伴可以跟隨小編一起學習一下2024-03-03
Android中實現(xiàn)WebView和JavaScript的互相調(diào)用詳解
這篇文章主要給大家介紹了關于Android中實現(xiàn)WebView和JavaScript的互相調(diào)用的相關資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友下面來一起看看吧。2018-03-03
Android開發(fā)自學筆記(六):聲明權限和Activity
這篇文章主要介紹了Android開發(fā)自學筆記(六):聲明權限和Activity,本文是上一篇的補充,需要的朋友可以參考下2015-04-04

