Android PopupWindow實現(xiàn)微信右上角的彈出菜單
日常開發(fā)過程中對于PopupWindown的使用也是比較多的。這里給大家展示一下PopupWindow的使用。
修改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" tools:context="${relativePackage}.${activityClass}" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dip" android:background="@android:color/holo_blue_dark"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dip" android:background="@drawable/ic_launcher" /> <ImageView android:id="@+id/rl_more" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/ability_show_item_bg" android:paddingLeft="15dp" android:paddingRight="5dp" android:layout_alignParentRight="true" android:src="@drawable/actionbar_more_icon" /> </RelativeLayout> </RelativeLayout>
新建popup_window.xml布局文件
<?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="match_parent" android:background="@android:color/white" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/settings" android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center" android:padding="12dp" android:text="設置" android:textSize="16sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#BDBDBD" /> <TextView android:id="@+id/about" android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center" android:padding="12dp" android:text="關(guān)于" android:textSize="16sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#BDBDBD" /> <TextView android:id="@+id/ability_logout" android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center" android:padding="12dp" android:text="退出" android:textSize="16sp" /> </LinearLayout>
自定義PopupWindow類PopWindow
package com.syz.mypopupwindow; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow; /** * <p>Title:PopWindow</p> * <p>Description: 自定義PopupWindow</p> * @author syz * @date 2016-3-14 */ public class PopWindow extends PopupWindow{ private View conentView; public PopWindow(final Activity context){ LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); conentView = inflater.inflate(R.layout.popup_window, null); int h = context.getWindowManager().getDefaultDisplay().getHeight(); int w = context.getWindowManager().getDefaultDisplay().getWidth(); // 設置SelectPicPopupWindow的View this.setContentView(conentView); // 設置SelectPicPopupWindow彈出窗體的寬 this.setWidth(w / 2 + 40); // 設置SelectPicPopupWindow彈出窗體的高 this.setHeight(LayoutParams.WRAP_CONTENT); // 設置SelectPicPopupWindow彈出窗體可點擊 this.setFocusable(true); this.setOutsideTouchable(true); // 刷新狀態(tài) this.update(); // 實例化一個ColorDrawable顏色為半透明 ColorDrawable dw = new ColorDrawable(0000000000); // 點back鍵和其他地方使其消失,設置了這個才能觸發(fā)OnDismisslistener ,設置其他控件變化等操作 this.setBackgroundDrawable(dw); // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); // 設置SelectPicPopupWindow彈出窗體動畫效果 this.setAnimationStyle(R.style.AnimationPreview); conentView.findViewById(R.id.about).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //do something you need here PopWindow.this.dismiss(); } }); conentView.findViewById(R.id.ability_logout).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // do something before signing out context.finish(); PopWindow.this.dismiss(); } }); conentView.findViewById(R.id.settings).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // do something you need here PopWindow.this.dismiss(); } }); } /** * 顯示popupWindow * * @param parent */ public void showPopupWindow(View parent) { if (!this.isShowing()) { // 以下拉方式顯示popupwindow this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5); } else { this.dismiss(); } } }
添加自定義PopupWindow所需的style
AnimationPreview
<style name="AnimationPreview"> <item name="android:windowEnterAnimation">@anim/fade_in</item> <item name="android:windowExitAnimation">@anim/fade_out</item> </style>
添加style所需的動畫
fade_in.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 左上角擴大--> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.001" android:toXScale="1.0" android:fromYScale="0.001" android:toYScale="1.0" android:pivotX="100%" android:pivotY="10%" android:duration="200" />
fade_out.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 左上角縮小 --> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="0.001" android:fromYScale="1.0" android:toYScale="0.001" android:pivotX="100%" android:pivotY="10%" android:duration="200" />
最后在MainActivity類中使用
package com.syz.mypopupwindow; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.rl_more).setOnClickListener(this); } @Override public void onClick(View v) { if(v.getId() == R.id.rl_more){ PopWindow popWindow = new PopWindow(this); popWindow.showPopupWindow(findViewById(R.id.rl_more)); } } }
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android自定義PopWindow實現(xiàn)QQ、微信彈出菜單效果
- Android仿微信長按菜單效果
- Android仿微信底部菜單欄效果
- Android制作微信app頂部menu菜單(ActionBar)
- Android中微信小程序開發(fā)之彈出菜單
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android開發(fā)Popwindow仿微信右上角下拉菜單實例代碼
- Android開發(fā)之微信底部菜單欄實現(xiàn)的幾種方法匯總
- Android仿微信底部菜單欄功能顯示未讀消息數(shù)量
- Android仿微信滑動彈出編輯、刪除菜單效果、增加下拉刷新功能
- Android仿微信加號菜單模式
相關(guān)文章
Android編程之基于Log演示一個activity生命周期實例詳解
這篇文章主要介紹了Android編程之基于Log演示一個activity生命周期,結(jié)合完整實例形式較為詳細的分析總結(jié)了Log演示activity生命周期的具體用法及Log的具體使用方法,需要的朋友可以參考下2015-12-12關(guān)于androidstuio導入系統(tǒng)源碼的問題
小編最近在做系統(tǒng)源碼導出來的小項目,在導入androidstudio過程中遇到過一些問題,本文以Schedule power on off為例給大家詳細介紹,需要的朋友參考下吧2021-06-06使用Kotlin+RocketMQ實現(xiàn)延時消息的示例代碼
這篇文章主要介紹了使用Kotlin+RocketMQ實現(xiàn)延時消息的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07Android應用開發(fā)中WebView的常用方法筆記整理
WebView即是在安卓本地應用中打開網(wǎng)頁視圖功能,其中對于JavaScript加載的各項操作是重點和難點,本文就為大家送上Android應用開發(fā)中WebView的常用方法筆記整理2016-05-05Android進階CameraX與Camera2使用比對詳解
這篇文章主要為大家介紹了Android進階CameraX與Camera2使用比示例對詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01