相对布局,将视图居中于父视图并将另一个视图右对齐且不重叠。

5
我正在尝试将一个TextView居中对齐在一个相对布局中,同时又位于一个ImageView的右侧。我希望它看起来像这样。
[-(image)-----some text here------------]

我可以使用下面的代码来实现此功能,但如果文本太长,则会重叠图像。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="@dimen/small_padding"
    android:paddingBottom="@dimen/small_padding"
    android:background="@color/White">

    <ImageView
        android:id="@+id/navMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_icon"
        android:layout_marginEnd="@dimen/small_padding"
        android:contentDescription="@string/abc_search_hint"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/actionBarTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/profile"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:maxLines="1"
        android:ellipsize="end"
        android:contentDescription="@string/title_activity_connect"
        android:layout_centerInParent="true" />

</RelativeLayout>

我尝试将TextView与ImageView右对齐,但这样做会导致它无法在父容器中居中。希望能得到帮助。


@DanielGruici 这是一个水平布局。图像视图必须完全靠左对齐,文本必须在父布局中居中,而不是在图像右侧的剩余空间中。当前代码的工作方式是有效的,直到文本变得过长,然后它会重叠到左侧的图像视图上。 - i_me_mine
我能想到的唯一方法是使用LinearLayout和权重。创建一个水平布局,左边是ImageView,中间是TextView,右边是空白视图。将图像和空白视图分配相等的权重,将更大的权重分配给文本视图。然后可以设置文本重力,始终居中,并受其他两个视图的限制。您可能需要调整权重以使其看起来符合您的要求。 - Daniel Gruici
@DanielGruici,我认为无论如何都必须采用一种hacky的解决方法。如果你在相对布局中强制一个视图在另一个视图的左侧或右侧,它会自动覆盖父级居中对齐。很不幸。 - i_me_mine
我找到的唯一解决方案是为文本提供maxLength并将其省略。这样它就不会与其他视图重叠了。 - Murat
你有找到解决方案吗?我也在寻找同样的答案。这是iOS操作栏的行为。标题始终居中于父级容器,但不会重叠任何内容。 - Emmanuel
显示剩余2条评论
3个回答

1
你可以尝试这样做。我使用了单选按钮组而不是线性布局,但它应该仍然有效。像你已经做的那样,将线性布局设置为水平,然后使布局重心居中,然后只需先放置图像再放置文本视图。
<RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:paddingTop="20dp">

                    <RadioButton
                        android:id="@+id/radio_student"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/checkbox_student"
                        android:onClick="onRadioButtonClicked"
                        android:layout_marginEnd="30dp"
                        android:layout_marginRight="30dp"/>

                    <RadioButton
                        android:id="@+id/radio_teacher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/checkbox_teacher"
                        android:onClick="onRadioButtonClicked"
                        android:layout_marginStart="30dp"
                        android:layout_marginLeft="30dp"/>

            </RadioGroup>

编辑: 我不知道我拥有的按钮的边距属性是否适用于文本视图,但是在文本的左侧填充可能会起作用。


我的问题是重叠,而不是对齐。此外,我没有使用线性布局,而是使用相对布局。这样我就可以将TextView居中放置在其中,同时将ImageView放置在左侧。由于有很多显示器和很多尺寸,使用边距来尝试解决问题是行不通的。 - i_me_mine

0

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="@dimen/small_padding"
    android:paddingBottom="@dimen/small_padding"
    android:background="@color/White">

    <ImageView
        android:id="@+id/navMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_icon"
        android:layout_marginEnd="@dimen/small_padding"
        android:contentDescription="@string/abc_search_hint"
        android:layout_alignParentStart="`enter code here`true" />

    <TextView
        android:id="@+id/actionBarTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/profile"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:maxLines="1"
        android:ellipsize="end"
        android:contentDescription="@string/title_activity_connect"
        android:layout_toRightOf="@+id/navMenu"
        android:alignParentRight="true"
        android:gravity="center" />

</RelativeLayout>

无法在相对布局中为元素设置重力。 - i_me_mine
1
我已经做到了,它可以使TextView中的文本居中显示。我需要将TextView居中显示在其父级RelativeLayout中。 - i_me_mine
忘记了什么,将TextView的layoutWidth设置为match_parent。 - fahad alawam
@fahadalawam,您误解了这个人的意图。这仍将使文本位于“TextView”的中心,而不是“RelativeLayout”。 - Jason Hu

0
你可以(而且为了性能,应该)为TextView使用复合绘图(compound drawable),并摆脱ImageView。
你想要图片保留在TextView的左侧,因此使用android:drawableLeft:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/small_padding"
    android:paddingBottom="@dimen/small_padding"
    android:background="@color/White"
    >
    <TextView
        android:id="@+id/actionBarTitle"
        android:drawableLeft="@drawable/home_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/profile"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:maxLines="1"
        android:ellipsize="end"
        android:contentDescription="@string/title_activity_connect"
        android:centerInParent="true"
        android:gravity="center"
    />    
</RelativeLayout>

1
所以这里的问题是,这也会将图像移动到布局的中心。如果我将宽度属性更改为匹配父级,则几乎可以解决问题。但是,然后文本将居中于imageview右侧的剩余空间中。我需要图像保持左对齐,文本保持在整个父布局内居中。这就是为什么这很棘手的原因。 - i_me_mine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接