在Android中缩小(或裁剪)对话框背景 [英] Scale down (or crop) dialog background in Android
问题描述
我有一个高分辨率图像(我们说1900x1200),我想用作我的对话框的背景。它被创建
对话框对话框=新的对话框(此);
dialog.setContentView(R.layout.dialog);
对话框布局:
< LinearLayout
android:id =@ + id / dialog
android:layout_width =wrap_content
android:layout_height =wrap_content
android:orientation =vertical>
但是,如果我使用此图像,则扩展对话框(布局)以覆盖图像(或达到全屏,以较小者为准)。如果我使图像非常小(像320x200),升序工作正常。
我需要,布局应该只包含内容,而不是背景图像。图像应该自动缩小或裁剪到对话框的大小,没关系,它是一个纹理。
我不想以编程方式设置/缩放(如果可以避免),因为我想在我所有的对话框中使用这个背景,我懒惰地更改代码,而不是XMLs :)
也绝对不想创建所有类型的分辨率,因为Dialog可以从非常小到全屏,所以没有有机会猜到这个大小。
我试过了:
添加到LinearLayout : android:background =@ drawable / your_image
这将扩展对话框以适合我不想要的图像。
使用FrameLayout。作为第一个孩子添加一个ImageView,并出现在LinerLayout中。在ImageView中设置以下内容。
android:src =@ drawable / your_image
android:scaleType =centerCrop//我尝试过所有scaleType
尝试所有的技巧:
<?xml版本=1.0encoding =utf-8?>
< RelativeLayout
android:layout_width =wrap_content
android:layout_height =wrap_content
xmlns:android =http://schemas.android.com/apk/ RES /机器人>
< 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>
I have a high res image (let's say 1900x1200), which I want to use as background for my dialog. It is created
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
Dialog layout:
<LinearLayout
android:id="@+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
However, if I use this image, the Dialog (layout) is expanded to cover the image (or reach the full-screen, whichever is smaller). If I make the image very small (like 320x200), the upscaling works fine. What I need, the Layout should wrap only the content, and NOT the background image. Image should be downscaled automatically or cropped to the dialog size, does not matter, it is a texture. I don't want to programmatically set/scale (if can be avoid), as I want to use this background in all my dialogs, and I am lazy to change code, instead of XMLs :) Also absolutely do not want to create all sort of resolutions, as Dialogs can be from very small to full screen, so no chance the guess the size.
What I tried:
Add to LinearLayout: android:background="@drawable/your_image
this will extend the dialog to fit the image, which I don't want.
Use FrameLayout. As a first child add an ImageView, and comes the LinerLayout. Set the following in your ImageView.
android:src="@drawable/your_image"
android:scaleType = "centerCrop" //I tried ALL scaleType
Tried all tricks here: Scale background image to wrap content of layout Did not work.
I believe there is a very simple solution for this very simple and frequent request, I just could not figure out.
Here is my full dialog layout source:
<?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 Here is an image for better understanding
The solution idea comes from here: 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>
这篇关于在Android中缩小(或裁剪)对话框背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!