[英]Black space in custom alert Dialog
I have designed a custom alert Dialog Box , but black space is coming on top & bottom of alert Box. 我设计了一个自定义警报对话框,但是警报框的顶部和底部有黑色空间。 my layout code is given below.I tried to give negative margin in my main LinearLayout but still problem exists. 我的布局代码如下。我试图在我的主要LinearLayout中给负的空白,但是仍然存在问题。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first_part"
android:orientation="horizontal"
android:layout_width="580px"
android:layout_height="90px"
android:background="#1c1c1c"
>
<ImageView android:id="@+id/ic_messageicon"
android:scaleType="fitXY"
android:layout_width="70px"
android:layout_height="70px"
android:layout_marginLeft="20px"
android:layout_marginTop="10px"
android:src="@drawable/ic_messagewarn"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25px"
android:layout_marginLeft="15px"
android:textSize="28px"
android:typeface="sans"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:text="@string/errorTitle"
></TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first_part_line"
android:orientation="horizontal"
android:layout_width="580px"
android:layout_height="1px"
android:background="#626262"
>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/second_part"
android:orientation="horizontal"
android:layout_width="580px"
android:layout_height="120px"
android:background="#252525"
>
<TextView android:id="@+id/messagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="sans"
android:textColor="#FFFFFF"
android:textSize="25px"
android:layout_marginLeft="20px"
android:layout_marginTop="20px"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/third_part_line"
android:orientation="horizontal"
android:layout_width="580px"
android:layout_height="1px"
android:background="#aaaaaa"
>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/third_part"
android:orientation="horizontal"
android:layout_width="580px"
android:layout_height="90px"
android:background="#999999"
>
<Button android:id="@+id/yes"
android:scaleType="fitXY"
android:layout_width="236px"
android:layout_height="57px"
android:layout_marginTop="16px"
android:onClick="onClick"
android:text="@string/yes"
android:textColor="#000000"
android:textSize="20px"
android:textStyle="bold"
android:layout_marginLeft="27px"
android:background="@drawable/messagebutton"
/>
<Button android:id="@+id/no"
android:scaleType="fitXY"
android:layout_width="236px"
android:layout_height="57px"
android:layout_marginTop="16px"
android:layout_marginRight="27px"
android:onClick="onClick"
android:text="@string/no"
android:layout_alignParentRight="true"
android:textColor="#000000"
android:textSize="20px"
android:textStyle="bold"
android:background="@drawable/messagebutton"
/>
</RelativeLayout>
xml in Values Folder and add this code 值文件夹中的xml并添加此代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
</style>
<style name="CustomDialogTheme" >
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:windowBackground">@color/transparent1</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowNoTitle">true</item>
</style>
and make a reference this Theme to Your Dialog Like this 并像这样将这个主题引用到您的对话框
Dialog dialog = new Dialog(activity, R.style.CustomDialogTheme);
then set Your custom Dialog Layout Xml file as setContentView . 然后将您的自定义对话框布局Xml文件设置为setContentView 。
dialog.setContentView(R.layout.customdialog);
f you look at the AlertDialog class source you'll see most of the methods are simply proxy methods (facade) around private AlertController mAlert. 如果您查看AlertDialog类的源代码,您将看到大多数方法只是围绕私有AlertController mAlert的代理方法(外观)。
Looking at the AlertController class source you'll see 4 interesting member variables: 查看AlertController类的源代码,您将看到4个有趣的成员变量:
private int mViewSpacingLeft;
private int mViewSpacingTop;
private int mViewSpacingRight;
private int mViewSpacingBottom;
private boolean mViewSpacingSpecified = false;
Setting mViewSpacingSpecified to true will remove the borders on the top and bottom of the dialog. 将mViewSpacingSpecified设置为true将删除对话框顶部和底部的边框。
This is done properly by changing this line: 更改此行即可正确完成此操作:
dialog.setView(layout);
to: 至:
dialog.setView(layout, 0, 0, 0, 0);
you are not closing the parent LinearLayout at the end 您没有在最后关闭父级LinearLayout
and you are using #000000 color code for parent layout this will display black color at background 并且您将#000000颜色代码用于父级布局,这将在背景上显示黑色
try to change the color code you bottom color will chance. 尝试更改颜色代码,您的底色将会出现。 and use 和使用
requestWindowFeature(Window.FEATURE_NO_TITLE);
in you dialog class this will remove the dialog title so your top color also will remove.. 在对话框类中,这将删除对话框标题,因此顶部颜色也将删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.