Android PopupWindow實現微信右上角的彈出菜單
更新時間:2020年09月17日 08:54:10 作者:Elven_Shi
這篇文章主要為大家詳細介紹了Android PopupWindow實現微信右上角的彈出菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
日常開發(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="關于" 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));
}
}
}
效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
Android編程之基于Log演示一個activity生命周期實例詳解
這篇文章主要介紹了Android編程之基于Log演示一個activity生命周期,結合完整實例形式較為詳細的分析總結了Log演示activity生命周期的具體用法及Log的具體使用方法,需要的朋友可以參考下2015-12-12
Android應用開發(fā)中WebView的常用方法筆記整理
WebView即是在安卓本地應用中打開網頁視圖功能,其中對于JavaScript加載的各項操作是重點和難點,本文就為大家送上Android應用開發(fā)中WebView的常用方法筆記整理2016-05-05
Android進階CameraX與Camera2使用比對詳解
這篇文章主要為大家介紹了Android進階CameraX與Camera2使用比示例對詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

