我怎么能水平对齐没有与两个Textviews? [英] How can I horizontally align two Textviews without overlapping?

查看:411
本文介绍了我怎么能水平对齐没有与两个Textviews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个N(新)徽章的的TextView 的结束。但是Android工作室给了我两个的TextView S, hashtag_list_row_title hashtag_list_row_new 可重叠,如果有一个的TextView 包含了一些冗长的字符串。

I'm trying to put an N (new) badge at the end of the Textview. But Android Studio gives me that two Textviews, hashtag_list_row_title and hashtag_list_row_new can overlap if one Textview contains some lengthy string.

在生产环境中,这是不太可能, hashtag_list_row_title 是漫长的,但我想prevent两个的TextView 被重叠秒。我怎样才能做到这一点?

In production environment, it's not likely that hashtag_list_row_title is long, but I want to prevent two Textviews from being overlapped. How can I achieve this?

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

I prefer the way hashtag_list_row_new has priority over hashtag_list_row_title because that's more natural way to display an N badge.

到目前为止,我的code是如下:

So far my code is as follows.

<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右侧应该将旁边的左边的一个,其具有变化的长度。我想prevent在那里,使正确的隐藏,或重叠,或任何左边变长超过一定长度的情况。

Edit

Textview on the right side should place right next to the left one, which has varying length. And I want to prevent the situation where the left one gets longer beyond certain length so that the right one is hidden, or overlapped, or whatever.

看起来应该如下图所示。

It should look as below.

| (文字),N(空空)|

| (text) N (empty space empty)|

| (更多的文字),N(空)|

| (more text) N (empty space) |

| (以上文字更多更多...)N |

| (more text more more...) N |

欲当它超越了行的宽度N除外徽章要ellipsized左侧TextView的。我想这一点。

I want the left TextView to be ellipsized when it's beyond the width of the row except for N badge. I want this.

我不希望这样。

ñ徽章不应放置在右侧的所有时间。我想旁边放置到左侧徽章的TextView 时,长度大于横向宽度短。

N badge should not be placed on the right side all the time. I want the badge to be placed right next to the left TextView when the length is shorter than horizontal width.

这是很好的。

这是没有去。

推荐答案

尝试类似下面。

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

结果将类似于像下面的东西。我不得不删除被指向我的计划没有一定的资源属性,所以下面的图只是一个模拟的。在code被更新,以适应你。

Result will be something similar like below. I had to remove some attributes that was pointing to resources that my project didn't have, so below figure is just a mock up. The code is updated to fit you.

这篇关于我怎么能水平对齐没有与两个Textviews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆