Android PopupWindow實(shí)現(xiàn)微信右上角的彈出菜單
日常開發(fā)過程中對(duì)于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="設(shè)置" 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();
// 設(shè)置SelectPicPopupWindow的View
this.setContentView(conentView);
// 設(shè)置SelectPicPopupWindow彈出窗體的寬
this.setWidth(w / 2 + 40);
// 設(shè)置SelectPicPopupWindow彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 設(shè)置SelectPicPopupWindow彈出窗體可點(diǎn)擊
this.setFocusable(true);
this.setOutsideTouchable(true);
// 刷新狀態(tài)
this.update();
// 實(shí)例化一個(gè)ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0000000000);
// 點(diǎn)back鍵和其他地方使其消失,設(shè)置了這個(gè)才能觸發(fā)OnDismisslistener ,設(shè)置其他控件變化等操作
this.setBackgroundDrawable(dw);
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
// 設(shè)置SelectPicPopupWindow彈出窗體動(dòng)畫效果
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所需的動(dòng)畫
fade_in.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 左上角擴(kuò)大--> <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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義PopWindow實(shí)現(xiàn)QQ、微信彈出菜單效果
- Android仿微信長按菜單效果
- Android仿微信底部菜單欄效果
- Android制作微信app頂部menu菜單(ActionBar)
- Android中微信小程序開發(fā)之彈出菜單
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android開發(fā)Popwindow仿微信右上角下拉菜單實(shí)例代碼
- Android開發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法匯總
- Android仿微信底部菜單欄功能顯示未讀消息數(shù)量
- Android仿微信滑動(dòng)彈出編輯、刪除菜單效果、增加下拉刷新功能
- Android仿微信加號(hào)菜單模式
相關(guān)文章
android TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了android TextView實(shí)現(xiàn)跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android編程之基于Log演示一個(gè)activity生命周期實(shí)例詳解
這篇文章主要介紹了Android編程之基于Log演示一個(gè)activity生命周期,結(jié)合完整實(shí)例形式較為詳細(xì)的分析總結(jié)了Log演示activity生命周期的具體用法及Log的具體使用方法,需要的朋友可以參考下2015-12-12
關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題
小編最近在做系統(tǒng)源碼導(dǎo)出來的小項(xiàng)目,在導(dǎo)入androidstudio過程中遇到過一些問題,本文以Schedule power on off為例給大家詳細(xì)介紹,需要的朋友參考下吧2021-06-06
使用Kotlin+RocketMQ實(shí)現(xiàn)延時(shí)消息的示例代碼
這篇文章主要介紹了使用Kotlin+RocketMQ實(shí)現(xiàn)延時(shí)消息的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android應(yīng)用開發(fā)中WebView的常用方法筆記整理
WebView即是在安卓本地應(yīng)用中打開網(wǎng)頁視圖功能,其中對(duì)于JavaScript加載的各項(xiàng)操作是重點(diǎn)和難點(diǎn),本文就為大家送上Android應(yīng)用開發(fā)中WebView的常用方法筆記整理2016-05-05
Android實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android自定義控件實(shí)現(xiàn)水波紋效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)水波紋效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android進(jìn)階CameraX與Camera2使用比對(duì)詳解
這篇文章主要為大家介紹了Android進(jìn)階CameraX與Camera2使用比示例對(duì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

