網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了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彈出式窗口的簡單使用。
原文鏈接:https://blog.csdn.net/lu202032/article/details/121608584
相關(guān)推薦
- 2022-10-27 Python?Pandas中布爾索引的用法詳解_python
- 2022-04-14 遇到一個git的大坑 src refspec master does not match any e
- 2022-06-12 C語言?詳細(xì)解析時間復(fù)雜度與空間復(fù)雜度_C 語言
- 2022-12-04 Python學(xué)習(xí)之字典和集合的使用詳解_python
- 2022-10-27 QT線程QThread的使用介紹_C 語言
- 2022-10-29 CSS實現(xiàn)單行、多行文本溢出顯示省略號的實現(xiàn)方法
- 2022-09-14 前端使用svg圖片改色實現(xiàn)示例_其它綜合
- 2023-07-10 Python使用MongoDB數(shù)據(jù)庫
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支