PopupWindow 彈出式窗口,在安卓中應用廣泛,相比於 PopupMenu 它更靈活,它的位置是可以自己調整的。
先看效果圖,點擊圓形頭像後會在底部彈出一個窗口,每個窗口中子項都可以作爲一個點擊事件,執行相應的功能。
每一個 PopupWindow 窗口就是一個佈局,所以需要一個佈局文件,然後去實現它的點擊事件。
1. 編寫主界面的佈局,activity_main.xml ,實現一個背景和圓形頭像(這裏的圓形頭像使用了開源庫roundedimageview,需要自己添加)添加地址:https://blog.csdn.net/weixin_43851639/article/details/90545273
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@drawable/girl2">
<com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/head"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:src="@drawable/head"
app:riv_border_color="#333333"
app:riv_border_width="1dp"
app:riv_oval="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="收藏"/>
</LinearLayout>
</LinearLayout>
2. 在 layout 目錄下新建一個佈局文件,命名爲 popupwindow。在裏面定義了三個按鈕。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/take_photo"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="拍照" />
<Button
android:id="@+id/choose_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相冊"/>
<Button
android:id="@+id/exit1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="取消"/>
</LinearLayout>
3. 在 MainActivity.java 中實現點擊事件,即點擊頭像,彈出窗口,然後獲取每一個窗口子項的點擊事件。
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private PopupWindow popupWindow = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取圖片id,然後設置監聽
imageView = (ImageView) findViewById(R.id.head);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindow();
}
});
}
//彈窗
private void showPopupWindow() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupWindowView = inflater.inflate(R.layout.popupwindow,null,false);
popupWindow = new
PopupWindow(popupWindowView,ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
//引入依附的佈局
View parentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
//相對於父控件的位置
popupWindow.showAtLocation(parentView,Gravity.BOTTOM,0,0);
//獲取焦點,否則無法點擊
popupWindow.setFocusable(true);
//按鈕點擊事件,此處獲取按鈕的id不同,因爲按鈕來自popupwindow的視圖
//拍照按鈕
Button takeBtn = popupWindowView.findViewById(R.id.take_photo);
takeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"拍照設置頭像",Toast.LENGTH_SHORT).show();
//調用拍照函數
popupWindow.dismiss();
takePhoto();
}
});
//相冊按鈕
Button chooseBt = popupWindowView.findViewById(R.id.choose_picture);
chooseBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"相冊選擇照片",Toast.LENGTH_SHORT).show();
//調用相冊函數
popupWindow.dismiss();
choosePicture();
}
});
//取消按鈕
Button exitBt = popupWindowView.findViewById(R.id.exit1);
exitBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"取消",Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
}
劃重點,劃重點,劃重點,怎麼獲取彈窗子項的 id,然後監聽他們的點擊事件。獲取按鈕id和以往不同,在上面代碼中有,這裏再寫出來。注意是在 popupWindowView 對象中獲取。
//定義一個對象 popupWindowView,承載彈窗佈局,然後需要在 popupWindowView 中獲取子項 id
View popupWindowView = inflater.inflate(R.layout.popupwindow,null,false);
...//省略中間代碼
//按鈕點擊事件,此處獲取按鈕的id不同,因爲按鈕來自popupwindow的視圖
Button takeBtn = popupWindowView.findViewById(R.id.take_photo);
到這裏彈窗的功能已經實現了,而且已經成功監聽了彈窗子項,至於每個子項實現的具體功能,要看自己的需求了。
https://blog.csdn.net/weixin_40430041/article/details/79099886