对话框大小与背景图像不匹配

时间:2015-06-09 16:11:12

标签: android android-dialog

我正在使用Android SDK制作游戏。一路上,我需要显示弹出/对话框,就像用户可以升级的任何其他游戏一样。我遇到的问题是对话框的大小。我正在使用RelativeLayout,我正在使用“wrap_content”将背景设置为我的图像。

对话框占用内部视图大小的问题(或Android设置的默认对话框最小大小,以较大者为准)不是背景图像。如果我使用fill_parent,那么它会拉伸它。我花了几个小时和几个小时旋转,我似乎无法找到一个有效的方式,窗口的大小与背景图像的大小相匹配

有什么建议吗?这是一个非常常见的用例,必须有办法! 感谢

以下是一些布局内容

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageButton
        android:id="@+id/ibCloseDialog"
        android:background="@null"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/close" />

 <Button
        android:id="@+id/b1"
        android:background="@drawable/blue_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/b2"
        android:text="b1" />
    <Button
        android:id="@+id/b2"
        android:background="@drawable/blue_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="b2" />
    <Button
        android:id="@+id/b3"
        android:background="@drawable/blue_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/b2"
        android:text="b2" />
</RelativeLayout>

6 个答案:

答案 0 :(得分:2)

我曾遇到过在全屏显示对话框的问题。我使用自定义样式删除对话框的默认样式(大小/边距/填充)。所以也许你可以使用它来包装你的内容而忽略默认值。所以请尝试以下方法:

1)在styles.xml中添加自定义Dialog主题:

<style name="YourDialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>

    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">false</item>

</style>

我认为高度,宽度和背景是这里的重要方面。也许你必须玩这些价值观。

2)使用以下代码段创建对话框:

Dialog dialog = new Dialog(context, R.style.YourDialogTheme)

答案 1 :(得分:0)

您使用的是atribute&#34; android:background&#34;你的RelativeLayout?如果是这种情况,您可以尝试这样做:

 <RelativeLayout
        .
        .
        .
        >
        <ImageView
            android:layout_width="MATCH_PARENT"
            android:layout_height="MATCH_PARENT"
            android:scaleType="fitXY"
            android:adjustViewBounds="true" 
            android:src="@drawable/yourDrawable" />    
    </RelativeLayout>
  1. 将ImageView放入相对布局中。
  2. 删除RelativeLayout的背景图片
  3. 将ImageView的src设置为背景图像
  4. 此外:

    • 制作正确大小的背景图像,并将它们保存在不同的可绘制文件夹中。
    • 指定不使用常量的视图大小,但在dp

答案 2 :(得分:0)

将单个大图像放在文件夹中并使用以下framelayout。添加到imgView

android:scaleType = "centerCrop"

答案 3 :(得分:0)

我不确定这是否是一种有效的解决方案,但由于对话框背景是资源文件,您可以获得背景的高度和宽度,并动态地将相同的高度和宽度设置为Dialog。像这样的东西

//get the dialog background drawable
Drawable popUpDrawable = getResources().getDrawable(R.drawable.popup);
//get height and width of drawable
int drawableWidth = popUpDrawable.getIntrinsicWidth();
int drawableHeight = popUpDrawable.getIntrinsicHeight();

//creating dialog
Dialog dialog = new Dialog (context);
...
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialogWindow.getAttributes());
//set the height and width of drawable as height and width of dialog
lp.width = drawableWidth;
lp.height = drawableHeight;
dialog.show();
//set the attributes to dialog
dialog.getWindow().setAttributes(lp);

答案 4 :(得分:0)

您可以使用src属性设置图像。我建议你设置为背景。

     <ImageButton
            android:id="@+id/ibCloseDialog"
            android:background="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            scaltype:centerInside
            android:background="@drawable/close" />

答案 5 :(得分:0)

我遇到了类似的问题。当子女孩的孩子右侧或底部对齐时,Android并不喜欢RelativeLayout基于孩子的大小。这被认为是一个循环引用&#34;虽然我同意这样的布局会有有效的用例。我使用FrameLayout

解决了这个问题
<FrameLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="..." />
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
        [ other views with their relative layout parameters]
    </RelativeLayout>
</FrameLayout>

在这个布局中,FrameLayout应该从图像视图中获取它的大小,然后RelativeLayout将匹配它。

相关问题