我如何水平对齐两个Textview而不重叠?

问题描述 投票:1回答:3

我正在尝试在Textview的末尾添加N(新)徽章。但是Android Studio告诉我,如果一个Textview包含一些长字符串,则两个hashtag_list_row_titlehashtag_list_row_newTextview可以重叠。

在生产环境中,hashtag_list_row_title不太可能长,但是我想防止两个Textview重叠。我该如何实现?

我更喜欢hashtag_list_row_new优先于hashtag_list_row_title的方式,因为这是显示N徽章的更自然的方式。

到目前为止,我的代码如下。

list_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hashtag_list_row_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_list_row_selector" >

    <RelativeLayout
        android:id="@+id/hashtag_list_row_title_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="24dp"
        android:layout_toLeftOf="@+id/hashtag_list_row_post_count"
        android:layout_toStartOf="@+id/hashtag_list_row_post_count">

        <TextView
            android:id="@+id/hashtag_list_row_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:textColor="@color/hotspot_list_row_title"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="asdfasdfsadfsadfsfsfsafasdfsafsfasfasfasf" />

        <TextView
            android:id="@+id/hashtag_list_row_new"
            android:layout_toRightOf="@+id/hashtag_list_row_title"
            android:layout_toEndOf="@+id/hashtag_list_row_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textStyle="bold"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="11dp"
            android:layout_marginStart="11dp"
            android:text="@string/N" />
    </RelativeLayout>

    <TextView
        android:id="@+id/hashtag_list_row_post_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@drawable/btn_post_normal"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#787878"
        android:textSize="12sp"
        android:textStyle="bold" />

    <View
        android:id="@+id/hashtag_list_row_divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/hashtag_list_row_title_layout"
        android:layout_marginTop="24dp"
        android:background="@color/bg_horizontal_divider" />

</RelativeLayout>

编辑

右侧的Textview应该紧靠左侧,其长度是可变的。而且我想避免这样一种情况,即左一个的长度超过一定长度,以至于右一个的隐藏,重叠或其他任何形式。

它看起来应该如下。

| (文本)N(空白为空)|

| (更多文字)N(空白)|

| (更多文字,更多...)N |

编辑2

我希望左边的TextView超出行的宽度时,将其椭圆化(N徽章除外)。我想要这个。

“

我不要这个。

“

编辑3

N徽章不应该一直放在右侧。当长度短于水平宽度时,我希望将徽标放置在左侧TextView的右侧。

很好。

“好”

不行。

“

android textview
3个回答
0
投票

遇到同样的问题。

您想要的是右侧的TextView:

android:layout_toEndOf="@+id/member_count"

该ID为左侧TextView的ID。

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/member_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:text="10"
            android:textSize="16sp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:layout_toEndOf="@+id/member_count"
            android:text="Members"
            android:textSize="16sp"/>

    </RelativeLayout>

-1
投票

使用LinearLayout代替RelativeLayout很容易

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hashtag_list_row_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/hashtag_list_row_title_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp">

        <TextView
            android:id="@+id/hashtag_list_row_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:text="dsfjsdk djkfhd hfdkjs afhdks hfkh  dsafkjhd fkjdh fdgf"
            android:textSize="16sp"
            android:layout_weight="1"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/hashtag_list_row_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="2dp"
            android:layout_toEndOf="@+id/hashtag_list_row_title"
            android:layout_toRightOf="@+id/hashtag_list_row_title"
            android:text="N"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/hashtag_list_row_post_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:singleLine="true"
            android:text="text2"
            android:textColor="#787878"
            android:textSize="12sp"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>

检查此修改后的代码


-1
投票

尝试以下类似方法。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hashtag_list_row_root"
    android:background="@drawable/bg_list_row_selector"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/hashtag_list_row_title_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="24dp"
        android:layout_toLeftOf="@+id/hashtag_list_row_post_count"
        android:layout_toStartOf="@+id/hashtag_list_row_post_count">

        <TextView
            android:singleLine="true" 
            android:id="@+id/hashtag_list_row_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:textColor="@color/hotspot_list_row_title"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="asdfasdfsadfsadfsfsfsafasdfsafsfasfasfasf" />

    </RelativeLayout>

    <TextView
        android:id="@+id/hashtag_list_row_post_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@drawable/btn_post_normal"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#787878"
        android:textSize="12sp"
        android:textStyle="bold" />

    <TextView
        android:layout_centerVertical="true"
        android:id="@+id/hashtag_list_row_new"
        android:layout_toLeftOf="@+id/hashtag_list_row_post_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/red"
        android:textStyle="bold"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:text="@string/N" />

    <View
        android:id="@+id/hashtag_list_row_divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/hashtag_list_row_title_layout"
        android:layout_marginTop="24dp"
        android:background="@color/bg_horizontal_divider" />

</RelativeLayout>

结果将类似于以下内容。我必须删除一些指向我项目没有的资源的属性,因此下图只是一个模型。代码已更新以适合您。

“

© www.soinside.com 2019 - 2024. All rights reserved.