【问题标题】:How to make dialog background image not expand dialog in android?如何使对话框背景图像不在android中展开对话框?
【发布时间】:2013-09-04 01:27:05
【问题描述】:

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

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

有人知道怎么做吗?

谢谢。

【问题讨论】:

    标签: android dialog background-image


    【解决方案1】:

    如果你在 xml 中设置它,你可以将宽度和高度设置为一个设定的量,像这样:

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

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

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

    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.
    

    【讨论】:

    • 有没有办法将其设置为fill_parent?
    • 如果是用xml布局的话,可以,但是父视图需要有一定的尺寸关联,不能是“matches_content”
    • 你可以测量你的父视图,这里是一个关于如何做到这一点的链接stackoverflow.com/a/4406090/793607 然后你可以将你的布局设置为那个宽度和高度。
    【解决方案2】:

    我找到了解决方案,请看这里: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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2013-03-09
        • 2023-01-13
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多