Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义对话框线性布局大小与对话框bg图像相同_Android_Layout_Dialog_Size - Fatal编程技术网

Android自定义对话框线性布局大小与对话框bg图像相同

Android自定义对话框线性布局大小与对话框bg图像相同,android,layout,dialog,size,Android,Layout,Dialog,Size,我正在创建一个自定义对话框,如下所示: Dialog myDialog = new Dialog(context, R.style.Theme_Levels); myDialog.setContentView(R.layout.levelselector); myDialog.show(); 使用的样式: <style name="Theme.Levels" parent="@android:style/Theme.Dialog"> <item

我正在创建一个自定义对话框,如下所示:

    Dialog myDialog = new Dialog(context, R.style.Theme_Levels);
    myDialog.setContentView(R.layout.levelselector);
    myDialog.show();
使用的样式:

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

@空的
真的
@可拉拔/dlgbg
@空的
状态未指定|调整盘
真的
假的
布局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>

我希望该按钮显示在对话框的底部中心(从底部开始填充50度),但它显示在对话框的左上角

这是因为LinearLayout不知道对话框的宽度/高度,这是我在样式xml文件中使用的背景图像的大小


问题:我如何知道对话框的宽度和高度,以便LinearLayout与对话框大小相同?

android:layout\u gravity=“bottom | center\u horizontal”
更改为
android:gravity=“bottom | center\u horizontal”



android:layout\u gravity
是布局本身的重力,即其父布局的重力
android:gravity
适用于其内容。

以下是如何做到这一点:

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

在我的案例中,我遇到了同样的问题,我最终使用了一个
RelativeLayout

给你

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

好的,我找到了一种方法,不是干净的方法,但很有效

您需要在style.xml文件中添加背景

   <item name="android:windowBackground">@drawable/levelselectbg</item> 
第一个背景需要保持对话框无边界(我在问题中的style.xml中指定了该背景),而linearlayout背景需要为布局提供大小,现在它知道了它的尺寸,并将按钮放置在底部中间,并添加了填充


希望这对其他人有所帮助,如果有更好的解决方案,请与大家分享。

为按钮设置布局。不,不幸的是,这不起作用。Inazaruk的回答起到了作用。它的底部有50度的填充,但它是左对齐的。确保
android:orientation=“vertical”
被添加到
LinearLayout
。它已经被添加。我使用的正是你发布的代码。另一个问题是背景图像被拉伸以覆盖整个屏幕的高度(我使用的是横向),尽管它比屏幕尺寸小。当我删除标签时,bg图像大小显示正确。您是否使用自定义对话框?您在relativelayout中指定了背景,而不是在样式xml中?在布局中放置bg时,我面临的主要问题是它的边框没有保持隐藏状态,我在样式xml文件中已将其设置为null。此布局通过在属性下方指定LAYOU来添加项目,如果我只有一个按钮在底部显示,而背景中只有一个图像,则此布局将不起作用。
android:background="@drawable/levelselectbg"