无法在RelativeLayout中对齐ImageView

Ika*_*Ika 2 android imageview android-relativelayout

我有一个包含两个 textView 和一个 ImageView 的布局。

第一个文本左对齐。第二个文本也向左对齐,位于第一个文本的下方。图像向右对齐!并与第一个文本的顶部和第二个文本的底部对齐。

我尝试使用相对布局来完成这项工作,但图像拒绝向右对齐。

这是我的 xml

<RelativeLayout
            android:id="@+id/NameAndPhoto"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:id="@+id/Names"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/FirstName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/contact_page_name_text_style" >
                </TextView>

                <TextView
                    android:id="@+id/LastName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/contact_page_name_text_style" >
                </TextView>
            </LinearLayout>

            <ImageView
                android:id="@+id/PersonPhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/Names"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@id/Names"
                android:layout_toRightOf="@id/Names"
                android:contentDescription="@string/contact_page_photo_content_description"
                android:padding="3sp"
                android:scaleType="fitCenter" >
            </ImageView>
        </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

请指教。

谢谢,伊卡。

Gan*_*nus 5

如果您想使用 LinearLayout,请width="match_parent"通过设置来使用和对齐gravity="right"。因此,您在具有父级宽度的视图中移动文本。

至于将 ImageView 放置在relativelayout 中,您会遇到一个矛盾:android:layout_alignParentRight="true"android:layout_toRightOf="@id/Names"- 两个不同的水平放置。

此外,您不能同时使用底部和顶部放置。您正在图像中使用它们。又是一个矛盾。

如何将 Image 放置在占据根布局所有宽度的 LinearLayout 的右侧?它可以放在哪里?将名称的宽度更改为wrap_content