将textview与图像的实际边界对齐

时间:2015-10-28 08:43:51

标签: android android-layout android-studio

我最近对ImageView对齐的问题感到困惑。我们知道ImageView的宽度 NOT 正好是实际图像的宽度(因为有时缩放图像)。那么如何将textview与实际图像的边界对齐,而不是ImageView?

+----------------------------------------+
|            +--------------+            |
|  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>

非常感谢!

1 个答案:

答案 0 :(得分: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>