2015-10-28 53 views
1

最近我對ImageView對齊問題感到困惑。我們知道ImageView的寬度是而不是正是實際圖像的寬度(因爲有時圖像被縮放)。那麼如何將文本視圖與的實際圖像的邊界對齊,而不是ImageView?將textview對齊圖像的實際邊界

+----------------------------------------+ 
|   +--------------+   | 
| ImageView | Scaled Image |   | 
|   +--------------+   | 
+----------------------------------------+ 

          | I want to align my textview to here 

             | Not here 

下面的代碼不符合我的要求,它總是TextView的對齊到的ImageView的權利,而不是實際的圖像:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <ImageView 
     android:id="@+id/mypic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/mypic" /> 
    <TextView 
     android:id="@+id/mytext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/mypic" /> 
</RelativeLayout> 

非常感謝!

+0

您需要scaletype,使其始終設置爲您的ImageView適合imageview。我不認爲你可以根據縮放的圖像設置你的textview(它對圖像不同) – Dhina

+0

請參閱'ImageView#getImageMatrix'和'ImageView#getDrawable'方法,然後仔細閱讀'Matrix' API – pskink

回答

0

我從您的文章瞭解什麼是要對齊的TextView到您的ImageView的右側,你可以像這樣:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

<ImageView 
    android:id="@+id/mypic" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/mypic" /> 
<TextView 
    android:id="@+id/mytext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/mypic" /> 
</RelativeLayout> 
+0

你有什麼區別b/w你的代碼和OP的? – Dhina