網站首頁 編程語言 正文
本文實例為大家分享了Android中PopupWindow彈出式窗口使用的具體代碼,供大家參考,具體內容如下
效果圖如下:
實現代碼如下:
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類實現代碼如下:
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);
? ? ? ? ? ? ? ? //設置彈出窗口應該接收外部觸摸事件
? ? ? ? ? ? ? ? popupWindow.setOutsideTouchable(true);
? ? ? ? ? ? ? ? //設置可聚焦
? ? ? ? ? ? ? ? popupWindow.setFocusable(true);
? ? ? ? ? ? ? ? popupWindow.showAsDropDown(btn_popupWindow);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
以上就是PopupWindow彈出式窗口的簡單使用。
原文鏈接:https://blog.csdn.net/lu202032/article/details/121608584
相關推薦
- 2022-04-12 Python數據可視化Pyecharts庫的使用教程_python
- 2022-12-21 Android?ChipGroup收起折疊效果實現詳解_Android
- 2022-07-30 沒有匹配的倉庫可以修改:PowerTools
- 2022-07-30 Linux secure 日志分析
- 2023-05-18 Python使用requirements.txt和pip打包批量安裝的實現_python
- 2022-12-27 python如何查找圖片按鈕的坐標位置_python
- 2023-01-13 Android?Parcleable接口的調用源碼層分析_Android
- 2023-05-17 mongodb?root用戶創建數據庫提示not?master的解決_MongoDB
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支