2014-09-02 97 views
2

我有一個高分辨率圖像(假設爲1900x1200),我想用它作爲我的對話框的背景。它創建在Android中縮小(或裁剪)對話框背景

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 :) 也絕對不想要創建各種分辨率,因爲對話框可以從非常小到全屏,所以沒有機會猜測大小。

我的嘗試:

加入的LinearLayoutandroid:background="@drawable/your_image這將擴大對話以適應圖像,這是我不想要的。

使用FrameLayout。作爲第一個孩子添加一個ImageView,並來到LinerLayout。在ImageView中設置以下內容。

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

嘗試所有的招數在這裏: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> 

編輯這裏是更好地瞭解圖像

enter image description here

+0

I不明白,如果你想你的形象**拉伸**,**裁剪**或**平鋪**。 – 2014-09-02 14:51:04

+0

由於圖像尺寸比對話框大得多,我希望將背景圖像拉伸或裁剪。相反,佈局正在擴展。 – Zotyi 2014-09-02 14:56:35

+0

你可以爲對話框顯示你的**完整佈局嗎?爲了看看要修復什麼。 – 2014-09-02 15:20:53

回答

2

該解決方案的想法來自這裏: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>