2012-11-04 44 views
0

我有一個可調整大小的應用部件。我想在圖片的上方和下方有文字。圖像應該佔用儘可能多的空間,文字應該直接放在圖像的上方和下方。可變父級大小的應用部件xml佈局

我只想在xml中做到這一點,但不能找出適用於所附圖像中顯示的兩種情況的佈局。

所以我的問題是,用什麼XML可以實現這種行爲?

編輯:圖像視圖可能比父容器視圖大。

desired layout behavior

+0

使用RelativeLayout和首先定義的圖像。在父級中使用中心對齊方式。然後是兩個textview,一個使用layoutAbove,另一個使用layoutBelow。 – Simon

回答

1

事情是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="center" 
     android:layout_alignParentCenter="true"/> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/imageview" 
     android:text="text 1"/> 


    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/imageview" 
     android:text="text 2"/> 


</RelativeLayout> 

還是這個,使用佈局重來控制大小:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:weightSum="100" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_weight="10" 
     android:layout_height="0px" 
     android:text="text 1"/> 

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="wrap_content" 
     android:layout_weight="80" 
     android:layout_height="0px" 
     android:scaleType="center" 
     android:layout_alignParentCenter="true"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_weight="10" 
     android:layout_height="0px" 
     android:text="text 2"/> 

</LinearLayout> 

播放使用權,影像對準和圖像比例鍵入以獲得它你想要的。

+0

感謝您的建議,但我已經嘗試過這種佈局,並且它不適用於場景1.圖像將填充所有可用高度,而不顯示文本視圖。 (順便說一句,我還必須添加adjustviewbounds = true,使您的場景2中的建議佈局工作) – timothyjc

+0

我編輯了另一個可能的解決方案。我還編輯了Re​​lativeLayout以包含scaleType。 – Simon

+0

再次感謝,但使用權重對情景1不起作用。藍色文字視圖要麼被切斷,要麼佔用更多空間,如果他們只是包裝其內容...... – timothyjc