Android 中缩小(或裁剪)对话框背景

2024-01-24

我有一个高分辨率图像(假设为 1900x1200),我想将其用作对话框的背景。它被创建了

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);

对话框布局:

<LinearLayout 
   android:id="@+id/dialog"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical" >

但是,如果我使用此图像,对话框(布局)将扩展以覆盖图像(或达到全屏,以较小者为准)。如果我将图像设置得非常小(例如 320x200),则放大效果很好。我需要的是,布局应该只包含内容,而不是背景图像。图像应该自动缩小或裁剪到对话框大小,没关系,它是纹理。我不想以编程方式设置/缩放(如果可以避免),因为我想在所有对话框中使用此背景,而且我懒于更改代码,而不是 XML :) 也绝对不想创建各种分辨率,因为对话框可以从非常小到全屏,所以不可能猜测大小。

我尝试过的:

添加到线性布局: android:background="@drawable/your_image这将扩展对话框以适应图像,这是我不想要的。

使用框架布局。作为第一个子元素,添加 ImageView,然后添加 LinearLayout。在 ImageView 中设置以下内容。

android:src="@drawable/your_image"
android:scaleType = "centerCrop" //I tried ALL scaleType

在这里尝试了所有技巧: 缩放背景图像以包裹布局内容 https://stackoverflow.com/questions/4761800/scale-background-image-to-wrap-content-of-layout不工作。

我相信对于这个非常简单且频繁的请求有一个非常简单的解决方案,我只是想不出来。

这是我的完整对话框布局源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:orientation="vertical"
android:minWidth="300dp" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back_dialog">

<EditText
    android:id="@+id/edittext_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<EditText
    android:id="@+id/edittext_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<CheckBox
    android:id="@+id/checkBox_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/IDS_ABC" />

<LinearLayout 
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button_ok"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/IDS_OK" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/IDS_CANCEL" />

</LinearLayout>
</LinearLayout>

EDIT这是为了更好地理解的图片


解决方案的想法来自这里:如何包装内容视图而不是背景可绘制? https://stackoverflow.com/questions/4312133/how-to-wrap-content-views-rather-than-background-drawable

<?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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中缩小(或裁剪)对话框背景 的相关文章

随机推荐