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

11

我正在使用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>

一定有人之前遇到过这个问题。 - Snake
尝试使用MATCH_PARENT - alexandreferris
请提供您的布局! - thiagolr
请参考所提供的布局:看到所提供的布局。 - Snake
@Snake 强烈建议阅读此问题的最佳答案。窗口布局参数并不是对话框过宽的唯一原因。您可能需要深入挖掘主题,以消除最小宽度,并修改导致扩展的装饰元素。使用hierarchyviewer在对话框布局/主题中准确定位多个原因。 - user1643723
显示剩余3条评论
6个回答

3

曾经我遇到一个问题,就是如何在全屏显示对话框。我使用了自定义样式来移除对话框默认的样式(大小/边距/内边距)。因此,也许您可以使用这种方法,在忽略默认设置的情况下包装您的内容。请尝试以下步骤:

1)在styles.xml中添加一个自定义的对话框主题:

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

我会尝试并回报。但是大小不是在基本对话框类的代码中设置而不是在xml中吗? - Snake
是的和不是。 Dialog有两个构造函数,Dialog(Context ctx)和Dialog(Context ctx, int style)。 如果您使用第一个构造函数,则将使用默认的style.xml。您可以在代码级别上设置样式属性,但这样做会更改/修改创建对话框时设置的属性(首先是xml,然后是您的代码)。通过使用自定义xml,您的属性从一开始就被设置。 - W3hri
仍然是同样的行为。我认为实际上这与将图像的alignParentRight设置为强制布局拉伸到全宽无关,即使父相对布局设置为包裹内容。看起来我遇到了与https://dev59.com/mWQm5IYBdhLWcg3wowWw完全相同的问题,但还没有解决方案:( - Snake

0
你在RelativeLayout中使用了"android:background"属性吗?如果是这样的话,你可以尝试这样做:
 <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设置为你的背景图片

另外:

  • 制作正确尺寸的背景图像,并将它们保存在不同的drawable文件夹中。
  • 指定视图的大小不要使用常量,而是使用dp。

这个问题无法解决。因为我正在使用像alignParentLeft和alignParentBottom这样的属性将内容放置在相对布局中。当你这样做时,对话框会超出图像大小并将组件放置在外部。 - Snake

0

将单个大图放入文件夹,并使用以下帧布局。添加到imgView中

android:scaleType = "centerCrop"

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

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

不,这没有任何区别。事实上,将其设置为背景会更加拉伸图像。 - Snake

0
我遇到了类似的问题。当RelativeLayout的子元素对齐到其右侧或底部时,Android不喜欢它根据其子元素进行大小调整。尽管我同意以这种方式布局会有有效的用例,但这被认为是“循环引用”。我已经使用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 应该从 ImageView 中获取其大小,然后 RelativeLayout 将匹配它。

我几乎确定我之前试过了,但我会再试一次并回报结果。 - Snake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接