3

在我的 android 项目中,当我在 xml 代码中为对话框设置背景时(对话框使用 xml 文件进行布局),对话框窗口大小会增加以适合其中的背景图像。

如何设置它以使窗口大小不增加。它只是忽略了不会出现在对话框中的背景部分。我猜图像的锚点可以是中心或左上角,没关系。

有谁知道如何做到这一点?

谢谢。

4

3 回答 3

1

如果您在 xml 中设置它,您只需将宽度和高度设置为设定的数量,如下所示:

    android:layout_width="100dp"
    android:layout_height="50dp"

或者,更好的是,使用尺寸,以便您可以调整不同的布局。或者您可以像下面这样手动设置它们。

这是 Ram kiran 在这个答案中使用的一个漂亮的小算法

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
于 2013-09-04T02:45:18.740 回答
1

我找到了解决方案,请参见此处:Scale down (or crop) dialog background in Android

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
  android:src="@drawable/back_dialog"
  android:scaleType="fitXY"
  android:layout_width="0dp"
  android:layout_height="0dp"
  android:layout_alignTop="@+id/dialog_layout_main"
  android:layout_alignBottom="@id/dialog_layout_main"
  android:layout_alignLeft="@id/dialog_layout_main"
  android:layout_alignRight="@id/dialog_layout_main" />
<LinearLayout 
  android:id="@+id/dialog_layout_main"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
   .........
</LinearLayout>
</RelativeLayout>
于 2014-09-02T19:21:17.073 回答
0

要么为父布局静态设置布局宽度和高度,要么为 ImageView 设置布局宽度和高度。设置 match_parent、wrap_content 或 fill_parent(deprecated) 将使对话框扩展到 ImageView。

于 2013-09-04T03:56:28.420 回答