LinearLayout未正确对齐

时间:2014-09-12 04:22:54

标签: android android-layout android-linearlayout

我有一个社交媒体图标和右边的用户名。我在整个屏幕上都有垂直线性布局,所以我希望图标的结尾和用户名的开头在中心排成一行。所以这就是我所做的:

<LinearLayout
        android:id="@+id/youtubeSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/youtubeIcon"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="right"
            android:layout_height="wrap_content"
            android:src="@drawable/youtube_circle_64" />

        <TextView
            android:id="@+id/youtubeTV"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="left"
            android:text="youtube_handle" />
    </LinearLayout>

我不明白为什么社交媒体图标仍然以它的重量为中心(距离屏幕宽度的四分之一)。 imageview应该占宽度的一半,然后右对齐它包含的图标,从而做我之前描述的。

1 个答案:

答案 0 :(得分:0)

一种选择是使用RelativeLayoutt来实现这种效果:

<LinearLayout
    android:id="@+id/youtubeSection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/youtubeIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/youtube_circle_64"
            android:layout_alignParentRight="true" />
    </RelativeLayout>
    <TextView
        android:id="@+id/youtubeTV"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="left"
        android:text="youtube_handle" />
</LinearLayout>

另一种选择是在TextView上设置左侧drawable(它具有消除大量布局复杂性的附加优势):

<TextView
    android:id="@+id/youtubeTV"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="left"
    android:drawableLeft="@drawable/youtube_circle_64"
    android:text="youtube_handle" />
相关问题