Android的定制对话框的LinearLayout大小相同的对话框背景图片 [英] Android custom dialog linearlayout size same as dialogs bg image

查看:204
本文介绍了Android的定制对话框的LinearLayout大小相同的对话框背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义对话框如下:

I am creating a custom dialog as:

    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>

布局xml文件:

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>

我想要的按钮,在出现的对话框中(从下50dip填充)的底部中心,但它出现在对话框的左上角。

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.

那是因为LinearLayout中不知道该对话框,这是我的风格XML文件中使用的背景图像的尺寸的宽度/高度。

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中是一样大小的对话框的大小

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

推荐答案

好吧,我已经找到一种方法,而不是一个干净的方式,但工作。

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

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

You need to add background in style.xml file

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

和也你的LinearLayout:

AND also in you linearlayout:

android:background="@drawable/levelselectbg"

第一个背景必须保持对话无国界(我在我的style.xml在我的问题规定)和的LinearLayout背景需要给大小的布局,现在它知道它的尺寸和放置按钮底部中心用填充。

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.

这篇关于Android的定制对话框的LinearLayout大小相同的对话框背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆