国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現底部彈出PopupWindow效果 iOS改變背景透明效果

2019-10-21 18:45:30
字體:
來源:轉載
供稿:網友

底部彈出PopupWindow,背景變為半透明效果,采用兩種方式實現

先來看看運行效果圖

 

iOS,底部彈出,PopupWindow,背景
iOS,底部彈出,PopupWindow,背景

[方式一]實現從底部彈出PopupWindow

原理:定義一個高度為wrap_content的PopupWindow布局文件,根據屏幕底部的位置顯示在Bottom

1.首先定義一個高度為wrap_content的PopupWindow布局文件

<?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="wrap_content"  android:gravity="center"  android:padding="8dp"  android:orientation="vertical" >  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:orientation="vertical"    android:background="@drawable/shape_info_bg">    <TextView android:layout_width="match_parent"      android:layout_height="wrap_content"      android:padding="8dp"      android:gravity="center"      android:textColor="@color/theme_blue"      android:text="拍照"/>    <View android:layout_width="match_parent"       android:layout_height="0.2dp"      android:background="@color/theme_blue"/>    <TextView android:layout_width="match_parent"      android:layout_height="wrap_content"      android:padding="8dp"      android:gravity="center"      android:textColor="@color/theme_blue"      android:text="相冊"/>  </LinearLayout>  <RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="5dp"    android:padding="1dp">    <Button      android:id="@+id/btn_cancle"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:gravity="center"      android:padding="8dp"      android:textColor="@color/theme_blue"      android:background="@drawable/shape_info_bg"      android:text="取消" />  </RelativeLayout></LinearLayout>

2.在PopupWindow所在的Activity根布局中聲明id(在彈出PopupWindow作為位置參考的相對View)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/root_main"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"   android:background="@color/white">  <Button android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="底部彈出PopupWindow"    android:onClick="openPop"/>   <Button android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="半透明底部PopupWindow"    android:onClick="openPop2"/></LinearLayout>

3.MainActivity中點擊按鈕彈出PopupWindow

/** 彈出底部對話框 */public void openPop(View view) {  View popView = LayoutInflater.from(mContext).inflate(      R.layout.pop_bottom, null);  View rootView = findViewById(R.id.root_main); // 當前頁面的根佈局  final PopupWindow popupWindow = new PopupWindow(popView,    LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);  setBackgroundAlpha(0.5f);//設置屏幕透明度  popupWindow.setBackgroundDrawable(new BitmapDrawable());  popupWindow.setFocusable(true);// 點擊空白處時,隱藏掉pop窗口  // 顯示在根佈局的底部  popupWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.LEFT, 0,      0);}

4.設置背景變為半透明效果,由于PopupWindow中并未設置背景色,所以彈出時PopupWindow以外的部分顯示當前Acitivity的內容,設置背景色原理通過設置當前Activity所在屏幕的透明度來達到背景透明的效果!在退出時popupWindow時,需要恢復屏幕原有透明度

popupWindow.setOnDismissListener(new OnDismissListener() {      @Override      public void onDismiss() {        // popupWindow隱藏時恢復屏幕正常透明度        setBackgroundAlpha(1.0f);      }    });
/** * 設置添加屏幕的背景透明度 *  * @param bgAlpha *      屏幕透明度0.0-1.0 1表示完全不透明 */public void setBackgroundAlpha(float bgAlpha) {  WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()      .getAttributes();  lp.alpha = bgAlpha;  ((Activity) mContext).getWindow().setAttributes(lp);}

[方式二]全屏PopupWindow實現底部彈出,背景半透明

原理:彈出一個全屏popupWindow,高度為match_parent,將根布局的Gravity屬性設置bottom,根布局背景設置為一個半透明的顏色#36000000, 彈出時全屏的popupWindow半透明效果背景覆蓋了在Activity之上 給人感覺上是popupWindow彈出后,背景變成半透明

布局文件

 

<?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:padding="8dp"  android:orientation="vertical"   android:background="#36000000"  android:gravity="bottom">  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:orientation="vertical"    android:background="@drawable/shape_info_bg">    <TextView android:layout_width="match_parent"      android:layout_height="wrap_content"      android:padding="8dp"      android:gravity="center"      android:textColor="@color/theme_blue"      android:text="拍照"/>    <View android:layout_width="match_parent"       android:layout_height="0.2dp"      android:background="@color/theme_blue"/>    <TextView android:layout_width="match_parent"      android:layout_height="wrap_content"      android:padding="8dp"      android:gravity="center"      android:textColor="@color/theme_blue"      android:text="相冊"/>  </LinearLayout>  <RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="5dp"    android:padding="1dp">    <Button      android:id="@+id/btn_cancle"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:gravity="center"      android:padding="8dp"      android:textColor="@color/theme_blue"      android:background="@drawable/shape_info_bg"      android:text="取消" />  </RelativeLayout></LinearLayout>

彈出方法和方式一相似,不過不用再監聽popupWindow的退出事件

/** * 彈出底部對話框,達到背景背景透明效果 *  * 實現原理:彈出一個全屏popupWindow,將Gravity屬性設置bottom,根背景設置為一個半透明的顏色, * 彈出時popupWindow的半透明效果背景覆蓋了在Activity之上 給人感覺上是popupWindow彈出后,背景變成半透明 */public void openPop2(View view) {  View popView = LayoutInflater.from(mContext).inflate(      R.layout.pop_bottom_alpha, null);  View rootView = findViewById(R.id.root_main); // 當前頁面的根佈局  final PopupWindow popupWindow = new PopupWindow(popView,      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  // 設置彈出動畫  popupWindow.setAnimationStyle(R.style.AnimationFadeBottom);  popupWindow.setBackgroundDrawable(new BitmapDrawable());  // 顯示在根佈局的底部  popupWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.LEFT, 0,      0);}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长春市| 邹城市| 大埔县| 汉川市| 大英县| 南康市| 阳朔县| 辽中县| 锡林浩特市| 株洲市| 尖扎县| 阿城市| 嘉禾县| 山阴县| 乌海市| 奎屯市| 镇巴县| 双鸭山市| 东阳市| 安阳市| 黔东| 恩平市| 五河县| 体育| 宜州市| 丹凤县| 平泉县| 九江市| 饶平县| 乐平市| 开鲁县| 鄂托克旗| 内黄县| 九江市| 莆田市| 临泽县| 霍邱县| 色达县| 灵丘县| 威海市| 桂平市|