Android custom dialog linearlayout size same as di

2019-07-18 17:00发布

问题:

I am creating a custom dialog as:

    Dialog myDialog = new Dialog(context, R.style.Theme_Levels);
    myDialog.setContentView(R.layout.levelselector);
    myDialog.show();

Style used:

<style name="Theme.Levels" parent="@android:style/Theme.Dialog">
   <item name="android:windowFrame">@null</item>
   <item name="android:windowIsFloating">true</item>
   <item name="android:windowBackground">@drawable/dlgbg</item>
   <item name="android:windowContentOverlay">@null</item>
   <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:backgroundDimEnabled">false</item>
</style>

Layout xml file:

<LinearLayout android:id="@+id/LevelSelector" 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" 
  android:layout_width="fill_parent" 
  android:layout_gravity="bottom|center_horizontal"
  android:paddingBottom="50dip"
  >     

    <Button android:text="Easy" 
        android:id="@+id/btn_Easy" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
    />
</LinearLayout>

I want the button to appear at bottom center of the dialog (50dip padding from bottom) but it appears at top left of the Dialog.

Thats because LinearLayout does not know the width/height of the dialog, which is the size of the background image I have used in style xml file.

Question: How do I know the width height of the dialog so that LinearLayout is of the same size as the Dialog size?

回答1:

Here is how you can do this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/LevelSelector" 
  android:layout_height="fill_parent" 
  android:layout_width="fill_parent" 
  android:orientation="vertical"
  android:paddingBottom="50dip" >     

    <View
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="1" />

    <Button android:text="Easy" 
        android:id="@+id/btn_Easy" 
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />

</LinearLayout>


回答2:

Change android:layout_gravity="bottom|center_horizontal" to android:gravity="bottom|center_horizontal".

android:layout_gravity is gravity of the layout itself w.r.t. its parent. android:gravity applies to its contents.



回答3:

i had the same problem in my case i ended up using a RelativeLayout

here it is.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/body" android:layout_margin="20dip"    
android:layout_width="wrap_content" android:layout_height="wrap_content"    
android:background="@drawable/dialog_bg" 
>

<TextView android:id="@+id/message"     
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent" android:layout_height="wrap_content"      
    android:layout_margin="10dip" 
    android:inputType="textMultiLine"       
    android:textColor="@color/table_text"
    android:textScaleX=".95" android:textStyle="bold"       
/>
<TextView android:id="@+id/dummy" android:layout_below="@id/message" 
    android:layout_width="fill_parent" android:layout_height="60dip"        
    android:visibility="invisible" 
/>  
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_margin="10dip" android:layout_below="@id/message" 
    android:layout_centerHorizontal="true" android:orientation="horizontal"
>
    <ImageButton android:id="@+id/ok" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:background="@drawable/button_alert" android:src="@drawable/btn_yes"
        android:visibility="gone" android:layout_weight=".5"
    />  
    <ImageButton android:id="@+id/cancel" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"    
        android:background="@drawable/button_alert" android:src="@drawable/btn_no"
        android:visibility="gone" android:layout_weight=".5"
    />
</LinearLayout>
</RelativeLayout>


回答4:

Ok, I have found a way, not a clean way but works.

You need to add background in style.xml file

   <item name="android:windowBackground">@drawable/levelselectbg</item> 

AND also in you linearlayout:

android:background="@drawable/levelselectbg"

The first background is required to keep the dialog borderless (which I have specified in my style.xml in my question) and the linearlayout background is required to give size to the layout, now it knows its dimensions and places the button at bottom center with padding.

Hope this helps someone, if a better solution is available please share.