简体   繁体   English

Android自定义对话框linearlayout大小与对话框bg图片相同

[英]Android custom dialog linearlayout size same as dialogs bg image

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: 布局xml文件:

<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. 我希望该按钮出现在对话框的底部中心(从底部开始50dip填充),但它出现在对话框的左上方。

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. 那是因为LinearLayout不知道对话框的宽度/高度,这是我在样式xml文件中使用的背景图像的大小。

Question: How do I know the width height of the dialog so that LinearLayout is of the same size as the Dialog size? 问题:如何知道对话框的宽度高度,以便LinearLayout与Dialog大小相同?

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>

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

android:layout_gravity is gravity of the layout itself wrt its parent. android:layout_gravity是布局本身及其父android:layout_gravity的重力。 android:gravity applies to its contents. android:gravity适用于其内容。

i had the same problem in my case i ended up using a RelativeLayout 我遇到同样的问题,我最终使用了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>

Ok, I have found a way, not a clean way but works. 好的,我找到了一种方法,虽然不是干净的方法,但是可以工作。

You need to add background in style.xml file 您需要在style.xml文件中添加背景

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

AND also in you linearlayout: 并且在您的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. 需要第一个背景来使对话框保持无边界(我在问题中的style.xml中指定了该对话框),并且需要linearlayout背景来给布局提供尺寸,现在它知道其尺寸并将按钮放在底部中心填充。

Hope this helps someone, if a better solution is available please share. 希望这对某人有帮助,如果有更好的解决方案,请分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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