Android中背景透明的Dialog

首先上一张图,看看是不是你想要的效果。
这里写图片描述
是你想找的效果么?那么就请往下看。
步骤:
1、首先我们先写一个布局文件。这个我就不多说了。直接上代码,写的很粗糙。
命名:dialog_search_service.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">

    <ImageView
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/dialog_search" />

    <Space
        android:id="@+id/space"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/search" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/space"
        android:gravity="center"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/progress"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="智能硬件搜索中..."
            android:textColor="@color/white"
            android:textSize="20sp" />

    </LinearLayout>
</RelativeLayout>

2、 在values中的style.xml中新建一个style样式。
代码如下:(重要)

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!--边框-->
        <item name="android:windowIsFloating">true</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">true</item>
        <!--半透明-->
        <item name="android:windowNoTitle">true</item>
        <!--无标题-->
        <item name="android:windowBackground">@color/transparent</item>
        <!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 如果设置android:backgroundDimEnabled为false.那弹出的对话框背景是亮的-->
        <item name="android:backgroundDimAmount">0.5</item>
    </style>

3、在Activity中使用,直接上代码。

Dialog dialog = new Dialog(LoginActivity.this, R.style.dialog);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_search_service, null);
dialog.setContentView(view);
//setting size
Window dialogWindow = this.getWindow();
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//手机横竖屏时候
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
    p.height = (int) (d.getHeight() * 0.2); // 高度设置为屏幕的
    p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的
}else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    p.height = (int) (d.getHeight() * 0.3); // 高度设置为屏幕的
    p.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的
}
dialogWindow.setAttributes(p);
dialog.show();

4、完工!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值