简体   繁体   中英

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 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 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 .

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.

Looking at the AlertController class source you'll see 4 interesting member variables:

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.

This is done properly by changing this line:

dialog.setView(layout);

to:

dialog.setView(layout, 0, 0, 0, 0);

referring to this link

you are not closing the parent LinearLayout at the end

and you are using #000000 color code for parent layout this will display black color at background

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM