Android中PopupWindow彈出式窗口使用方法詳解
本文實(shí)例為大家分享了Android中PopupWindow彈出式窗口使用的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下:
實(shí)現(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類實(shí)現(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彈出式窗口的簡(jiǎn)單使用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)之PopupWindow實(shí)現(xiàn)彈窗效果
- Android彈窗ListPopupWindow的簡(jiǎn)單應(yīng)用詳解
- Android使用 PopupWindow 實(shí)現(xiàn)底部彈窗功能
- Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
- Android開(kāi)發(fā)實(shí)現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法
- Android Popupwindow彈出窗口的簡(jiǎn)單使用方法
- Android編程實(shí)現(xiàn)的自定義彈窗(PopupWindow)功能示例
- Android自定義彈出窗口PopupWindow使用技巧
- Android控件PopupWindow模仿ios底部彈窗
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
相關(guān)文章
Android App中使用AudioManager類來(lái)編寫(xiě)音頻播放器
這篇文章主要介紹了Android App中使用AudioManager類來(lái)編寫(xiě)音樂(lè)播放器的方法,文中舉了一個(gè)簡(jiǎn)單的例子實(shí)現(xiàn)了基礎(chǔ)的播放暫停和靜音等功能,需要的朋友可以參考下2016-04-04Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家介紹了Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)的圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
今天給大家?guī)?lái)的是僅僅使用一個(gè)TextView實(shí)現(xiàn)一個(gè)高仿京東、淘寶、唯品會(huì)等各種電商APP的活動(dòng)倒計(jì)時(shí)。今天小編把實(shí)現(xiàn)代碼分享到腳本之家平臺(tái),對(duì)android textclock 倒計(jì)時(shí)效果感興趣的朋友參考下吧2016-10-10Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子,例子邏輯清晰,但相對(duì)簡(jiǎn)單了些,需要的朋友可以參考下2014-07-07Android開(kāi)發(fā)之開(kāi)發(fā)者頭條(二)實(shí)現(xiàn)左滑菜單
本文給大家介紹Android開(kāi)發(fā)之開(kāi)發(fā)者頭條(二)實(shí)現(xiàn)左滑菜單,主要用android自帶的DrawerLayout控件實(shí)現(xiàn)的此功能,具體實(shí)現(xiàn)過(guò)程請(qǐng)參考下本文2016-04-04Android利用滑動(dòng)菜單框架實(shí)現(xiàn)滑動(dòng)菜單效果
這篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)菜單特效之滑動(dòng)菜單框架完全解析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android Loader詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了Android Loader詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12