2013-12-11 21 views
0

我有一個layout_height設置爲wrap_content的線性佈局,所以佈局高度取決於文本行數。未改變父級佈局的圖像背景

問題(這讓我瘋狂的整個上午)是我想設置一個圖像作爲佈局的背景,但爲了確保圖像可以使用任何佈局的大小,我已經使圖像相當身高很大。目標是如果佈局很小,圖像將在高度上裁剪。但我無法做到這一點:無論我嘗試,佈局是根據圖像高度調整大小。

那麼,如何設置圖像作爲背景的東西,但裁剪本身,以便它不重新佈置父佈局(當然,我不知道父佈局的大小,因爲它取決於內部的文本) 。

由於英語不是我的第一語言,希望我的問題是清楚的...

預先感謝您。

編輯:一些代碼示例和截圖:

這是原來的卡片,而不會在背景圖像。

Original

這是添加了圖像視圖代碼:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/drill_background_img" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

    ... [Card Content Here] 

    </LinearLayout> 

</FrameLayout 

...,結果是我的名片被拉伸到我的形象的高度。但我不想那樣。我想要這張卡的大小相同。但我無法降低圖像高度,因爲卡片可以有更多的文字並且更高,所以我的圖像必須更高。

enter image description here

+0

這將是有益的,如果你在這裏發佈任何截圖螞蟻佈局xml文件。 –

+0

請提供佈局.... –

+0

我已經測試了很多不同的組合,所以我沒有正確的佈局顯示。但好的,我將編輯上面的代碼併發佈一個不起作用的示例。 –

回答

0

我不認爲你可以使用它作爲任何視圖「背景」屬性時,裁剪圖像。 圖片將始終被拉伸以填充整個背景。通常背景你應該使用Nine Patches。熟悉它,也許你可以改變你的功能或UI規範來使用它們而不是普通的圖像。 我知道修剪圖像的唯一方法是在ImageView中,它的圖像由「src」設置,而不是「背景」。

我通常會避免在可更改大小的組件中使用固定大小的圖像。你還必須記住,在Android中,當你考慮不同的方向,屏幕大小和屏幕密度時,幾乎所有的東西都會改變大小。

+0

我不能有一個9補丁,不適合我的形象。 –

0

我認爲背景總是調整大小以適應容器大小。但是你可以使用FrameLayout或者RelativeLayout和ImageView。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clipChildren="true"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     ...your content here... 
    </LinearLayout> 
</RelativeLayout> 
+0

我試過了,它不起作用... –

1

這是線性佈局的代碼,它將居中並調整文本內部的大小。 我已經測試過它。

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:orientation="vertical" 
    android:layout_centerHorizontal="true" 
    android:layout_marginLeft="25dp" 
    android:layout_marginRight="25dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvInfo" 
     android:textColor="#ffffff" 
     android:background="@drawable/toast_frame" 
     android:gravity="center" /> 
</LinearLayout> 

給它一個嘗試它應該工作。在這裏,我使用的圖像足夠小以適應最小的尺寸。 Android會自動加固。我爲這種情況使用9patch圖像。爲了更多地瞭解9patch訪問這個Link from developer.android.com

Simple online tool to generate 9patch 還要檢查this SO Answer

我已經在這裏uploded toast_frame.9.png文件,這樣就可以用我上面
**編輯** 變化

給代碼測試
<FrameLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@drawable/drill_background_img" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

... [Card Content Here] 

</LinearLayout> 

</FrameLayout> 

<FrameLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/drill_background_img"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

... [Card Content Here] 

</LinearLayout> 

</FrameLayout> 

我希望這會起作用。

+0

謝謝你的回答,但如果我理解正確,那與我的問題沒有任何關係。請參閱上面編輯的代碼。我的圖像不能是9的補丁,而且卡可能很大,所以圖像必須很大。 –

+0

@Stéphane請檢查我上面編輯的答案,並讓我知道它是否有幫助! – dmSherazi

+0

非常感謝您的回答。不幸的是,它沒有改變任何東西。我還試圖把它包裝在一個位圖: '<位圖的xmlns:機器人= 「http://schemas.android.com/apk/res/android」 \t機器人:SRC = 「@繪製/ drill_background_img」 \t android:gravity =「top | right | clip_vertical」/>' ...但它沒有幫助。 –