半透明背景Activity实现AlertDialog弹窗效果

来源:互联网 发布:试用平台源码 编辑:程序博客网 时间:2024/05/23 00:04

用半透明背景Activity实现AlertDialog弹窗效果的原因是,用小米手机测试项目时,发现工具类中的AlertDialog弹窗无法弹出,查阅资料才知道小米工程师为了防止有人作恶禁用了这项功能。那么,现在必须想出替换方案,实现类似AlertDialog的弹窗效果。

最后决定用半透明背景Activity实现AlertDialog的弹窗效果。
看下最后的效果图:
这里写图片描述
先给出代码,之后分析:

MainActivity.java

public class Main2Activity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏        setContentView(R.layout.activity_main2);    }}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/transparent">    <RelativeLayout        android:id="@+id/message_relativelayout"        android:layout_centerInParent="true"        android:background="@color/white"        android:layout_width="300dp"        android:layout_height="100dp">        <TextView            android:id="@+id/message"            style="@style/text_s36_000000"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:text="蓝牙设备已断开连接" />    </RelativeLayout>    <RelativeLayout        android:id="@+id/menu"        android:layout_below="@id/message_relativelayout"        android:layout_centerInParent="true"        android:background="@color/white"        android:layout_width="300dp"        android:layout_height="50dp">        <View            android:layout_width="fill_parent"            android:layout_height="1px"            android:layout_alignParentTop="true"            android:background="@color/cdddddd"/>        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_centerVertical="true">            <RelativeLayout                android:id="@+id/cancel"                android:layout_width="match_parent"                android:layout_height="49dp"                android:background="@drawable/white_selector"                android:layout_weight="1" >                <LinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerHorizontal="true"                    android:layout_centerVertical="true">                    <TextView                        android:id="@+id/cancel_text"                        style="@style/text_s28_1c1c1c"                        android:layout_gravity="center_vertical"                        android:text="取消"/>                </LinearLayout>            </RelativeLayout>            <View                android:layout_width="1dp"                android:layout_height="25dp"                android:layout_gravity="center"                android:background="@color/cdddddd"/>            <RelativeLayout                android:id="@+id/shutdown"                android:layout_width="match_parent"                android:layout_height="49dp"                android:background="@drawable/white_selector"                android:layout_weight="1" >                <LinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerHorizontal="true"                    android:layout_centerVertical="true">                    <TextView                        android:id="@+id/soutdown_text"                        style="@style/text_s28_1c1c1c"                        android:layout_gravity="center_vertical"                        android:text="关闭蓝牙"/>                </LinearLayout>            </RelativeLayout>            <View                android:layout_width="1dp"                android:layout_height="25dp"                android:layout_gravity="center"                android:background="@color/cdddddd"/>            <RelativeLayout                android:id="@+id/reconnect"                android:layout_width="match_parent"                android:layout_height="49dp"                android:background="@drawable/white_selector"                android:layout_weight="1" >                <LinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerHorizontal="true"                    android:layout_centerVertical="true">                    <TextView                        android:id="@+id/reconnect_text"                        style="@style/text_s28_1c1c1c"                        android:layout_gravity="center_vertical"                        android:text="重新连接"/>                </LinearLayout>            </RelativeLayout>        </LinearLayout>        <View            android:layout_width="fill_parent"            android:layout_height="1dp"            android:layout_alignParentBottom="true"            android:background="@color/cdddddd"/>    </RelativeLayout></RelativeLayout>

colors.xml

<color name="black_overlay">#66000000</color>

styles.xml

<style name="Transparent"  parent="Theme.AppCompat.Light.DarkActionBar">    <item name="android:windowBackground">@color/black_overlay</item>    <item name="android:windowIsTranslucent">true</item>    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item></style>

AndroidManifest.xml

<activity android:name=".ui.ShowBlueToothCheckDialogActivity"    android:theme="@style/Transparent" ></activity>

MainActivity.java和activity_main.xml是仿弹窗布局,很简单不再说明,只要注意把弹窗布局放在窗口中间就行。

这里用到android:theme来实现半透明,半透明效果的实现是styles.xml,其中用到了<item name="android:windowBackground">@color/black_overlay</item>,设置windowBackground为浅灰色,没有设置成全透明<color name="transparent">#00000000</color>,这是为了看上去有个遮罩效果,可以更明显的区分弹窗和背景。

原创粉丝点击