2013-07-12 94 views
2

是否有人制造TextView width匹配複合可繪製寬度(XML)?TextView寬度匹配drawableTop寬度

例如,對於XML代碼:

<TextView 
    android:id="@+id/textView" 
    style="@style/TextIcons" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/icon_contact" 
    android:gravity="center" 
    android:lines="2" 
    android:text="@string/contact_label" /> 

我得到:

|---------------------| 
| |-------------| | 
| |    | | 
| | Drawable | | 
| | View  | | 
| |    | | 
| |-------------| | 
|[---Contact Label---]| 
|---------------------| 

但我真正想要的是:

|-----------------| 
| |-------------| | 
| |    | | 
| | Drawable | | 
| | View  | | 
| |    | | 
| |-------------| | 
| [---Contact---] | 
| [----Label----] | 
|-----------------| 

回答

4

你可以簡單地把它通過分離來工作圖像和TextView,轉換爲相對佈局。這使得很容易指定對準一些視圖的邊緣等

下面的代碼應該做一些接近你想要什麼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/icon_contact" 
      android:id="@+id/imageView" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp"> 

    </ImageView> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="@string/contact_label" 
      android:id="@+id/textView" 
      android:layout_below="@+id/imageView" 
      android:layout_alignRight="@+id/imageView" 
      android:layout_alignLeft="@+id/imageView"/> 
</RelativeLayout> 
+0

它的工作原理,但實際上我得到了一致的兩條線左側,中間是必須的。 – GLopez

+0

我明白了,在TextView中只添加android:gravity =「center」,謝謝。我寧願更簡單但看起來不是。 – GLopez