【问题标题】:Dialog size does not match the background image对话框大小与背景图片不匹配
【发布时间】:2015-08-24 14:08:36
【问题描述】:

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

【问题讨论】:

  • 一定有人遇到过这种情况
  • 尝试使用MATCH_PARENT
  • 看看这个答案,它可能会对你有所帮助。 stackoverflow.com/a/6631310/2071847
  • @holpducki match_parent 和 fill_parent 是一样的。一样的,新的名字
  • @amitfarag 哦!好久没碰Android 应用了

标签: android android-dialog


【解决方案1】:

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

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) 使用这个 sn-p 创建你的对话框:

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

【讨论】:

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

您是否在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 设置为背景图片

还有:

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

【讨论】:

  • 这个不行的问题。因为我使用 alignParentLeft 和 alignParentBottom 等属性将内容放置在相对布局中。当你这样做时,对话框会超出图像大小并将组件放在外面
【解决方案3】:

将单个大图像放入文件夹并使用以下框架布局。添加到imgView

android:scaleType = "centerCrop"

【讨论】:

    【解决方案4】:

    我不确定这是否是一个有效的解决方案,但由于对话框背景是一个资源文件,您可以获取背景的高度和宽度,并将相同的高度和宽度动态设置为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);
    

    【讨论】:

      【解决方案5】:

      您使用 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" />
      

      【讨论】:

      • 不,没有区别。将事实设置为背景进一步拉伸图像
      【解决方案6】:

      我遇到了类似的问题。 Android 不喜欢 RelativeLayout 在其子级对齐其右侧或底部时根据其子级调整大小。这被认为是“循环引用”,尽管我同意以这种方式布局会有有效的用例。我使用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 会匹配它。

      【讨论】:

      • 我几乎可以肯定我之前尝试过,但我会再试一次并报告
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2014-06-20
      • 2014-06-16
      相关资源
      最近更新 更多