Android中背景透明的Dialog

来源:互联网 发布:winxp哪个版本适合编程 编辑:程序博客网 时间:2024/05/22 17:56

首先上一张图,看看是不是你想要的效果。
这里写图片描述
是你想找的效果么?那么就请往下看。
步骤:
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 sizeWindow 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、完工!

0 0
原创粉丝点击