Android中PopupWindow彈出式窗口使用方法詳解
本文實例為大家分享了Android中PopupWindow彈出式窗口使用的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下:

實現(xiàn)代碼如下:
activity_popup_window.xml按鈕
<?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=".PopupWindowActivity"> ? ? <Button ? ? ? ? android:id="@+id/btn_popupWindow" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:text="PopupWindow" /> </LinearLayout>
自定義彈出的視圖layout_pop.xml,也可以用RecycleView或者ListView
<?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:orientation="vertical"> ? ? <TextView ? ? ? ? android:id="@+id/tv_good" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="center" ? ? ? ? android:paddingTop="8dp" ? ? ? ? android:paddingBottom="8dp" ? ? ? ? android:text="好" ? ? ? ? android:textColor="@color/gray" ? ? ? ? android:textSize="20sp" /> ? ? <View ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="0.5dp" ? ? ? ? android:background="@color/gray" /> ? ? <TextView ? ? ? ? android:id="@+id/tv_not_too_bad" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="center" ? ? ? ? android:paddingTop="8dp" ? ? ? ? android:paddingBottom="8dp" ? ? ? ? android:text="還行" ? ? ? ? android:textColor="@color/gray" ? ? ? ? android:textSize="20sp" /> ? ? <View ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="0.5dp" ? ? ? ? android:background="@color/gray" /> ? ? <TextView ? ? ? ? android:id="@+id/tv_bad" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="center" ? ? ? ? android:paddingTop="8dp" ? ? ? ? android:paddingBottom="8dp" ? ? ? ? android:text="不好" ? ? ? ? android:textColor="@color/gray" ? ? ? ? android:textSize="20sp" /> </LinearLayout>
PopupWindowActivity類實現(xiàn)代碼如下:
public class PopupWindowActivity extends AppCompatActivity {
? ? private Button btn_popupWindow;
? ? private PopupWindow popupWindow;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_popup_window);
? ? ? ? btn_popupWindow = findViewById(R.id.btn_popupWindow);
? ? ? ? btn_popupWindow.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? View popup_view = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.layout_pop, null);
? ? ? ? ? ? ? ? TextView textView = popup_view.findViewById(R.id.tv_good);
? ? ? ? ? ? ? ? textView.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? ? ? ? ? popupWindow.dismiss();
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(PopupWindowActivity.this, "好", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? popupWindow = new PopupWindow(popup_view, btn_popupWindow.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
? ? ? ? ? ? ? ? //設(shè)置彈出窗口應(yīng)該接收外部觸摸事件
? ? ? ? ? ? ? ? popupWindow.setOutsideTouchable(true);
? ? ? ? ? ? ? ? //設(shè)置可聚焦
? ? ? ? ? ? ? ? popupWindow.setFocusable(true);
? ? ? ? ? ? ? ? popupWindow.showAsDropDown(btn_popupWindow);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}以上就是PopupWindow彈出式窗口的簡單使用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之PopupWindow實現(xiàn)彈窗效果
- Android彈窗ListPopupWindow的簡單應(yīng)用詳解
- Android使用 PopupWindow 實現(xiàn)底部彈窗功能
- Android PopupWindow實現(xiàn)左側(cè)彈窗效果
- Android開發(fā)實現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法
- Android Popupwindow彈出窗口的簡單使用方法
- Android編程實現(xiàn)的自定義彈窗(PopupWindow)功能示例
- Android自定義彈出窗口PopupWindow使用技巧
- Android控件PopupWindow模仿ios底部彈窗
- android PopupWindow 和 Activity彈出窗口實現(xiàn)方式
相關(guān)文章
Android開發(fā)之在程序中時時獲取logcat日志信息的方法(附demo源碼下載)
這篇文章主要介紹了Android開發(fā)之在程序中時時獲取logcat日志信息的方法,結(jié)合實例形式較為詳細(xì)的分析了實時獲取logcat日志的原理、步驟與相關(guān)實現(xiàn)技巧,并附帶相應(yīng)的demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02
Android實現(xiàn)文字翻轉(zhuǎn)動畫的效果
本文實現(xiàn)了Android程序文字翻轉(zhuǎn)動畫的實現(xiàn),具有一定的參考價值,有需要的朋友可以了解一下。2016-10-10
Android實現(xiàn)錄音監(jiān)聽動畫的示例代碼
在很多app種內(nèi)置了語音助手,也存在各種動畫,這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)錄音監(jiān)聽動畫的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Android拼圖游戲 玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化
這篇文章主要介紹了Android拼圖游戲的實現(xiàn)方法,教大家玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
詳解Android中Service服務(wù)的基礎(chǔ)知識及編寫方法
這篇文章主要介紹了詳解Android中Service服務(wù)的基礎(chǔ)知識及編寫方法,包括Service的啟動流程及生命周期等基本內(nèi)容,需要的朋友可以參考下2016-04-04
android panellistview 圓角實現(xiàn)代碼
android panellistview 圓角是每一個android開發(fā)者都具備的一項,對于新手朋友來說可能有點難度,接下來將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12
Android ListView自定義Adapter實現(xiàn)仿QQ界面
這篇文章主要為大家詳細(xì)介紹了ListView自定義Adapter實現(xiàn)仿QQ界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android編程實現(xiàn)PendingIntent控制多個鬧鐘的方法
這篇文章主要介紹了Android編程實現(xiàn)PendingIntent控制多個鬧鐘的方法,涉及PendingIntent屬性設(shè)置與使用的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12

