如何在左侧的一行中对齐两个TextView

时间:2017-01-24 08:44:55

标签: android layout textview

我应该在一个通道中对齐两个TextView。但是第一个TextView应该在它太长时间内被裁剪。

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/subtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    tools:text="subtitle" />

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/subtitle"
                    android:maxLines="1"
                    tools:text=" * date" />
            </RelativeLayout>

我有问题。当第一个TextView文本太长时,第二个TextView就会被隐藏。

First case. Short subtitle

Second case. Long subtitle. Second TextView is cropped

短文时,两个视图都应放在左侧。但是当文本很长时,应该裁剪第一个文本。

9 个答案:

答案 0 :(得分:1)

将布局与weight

一起使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/subtitle"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:maxLines="1"
    android:text="subtitlevvnvnvnnvnvnvnvnvnvnvnvnvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
    android:ellipsize="end"
    android:layout_weight="0.5" />

<TextView
    android:id="@+id/date"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:maxLines="1"
    android:text=" * date"
    android:layout_weight="0.5" />
</LinearLayout>

输出:

enter image description here

答案 1 :(得分:0)

在TextView

中添加填充
android:paddingLeft="10dp"

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:paddingLeft="10dp"
                tools:text="subtitle" />

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/subtitle"
                android:maxLines="1"
                android:paddingLeft="10dp"
                tools:text=" * date" />
        </RelativeLayout>

答案 2 :(得分:0)

这样做可能有助于同等规模。我给了他们布局权重,你可以改变它并相应地调整文本视图。您可以从here

了解有关线性布局权重的更多信息
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <TextView
            android:id="@+id/subtitle"
            style="@style/m4w_TextView.Subtitle"
            fontPath="fonts/InterstatePro-Light.otf"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:layout_weight="1"
            tools:text="subtitle" />

        <TextView
            android:id="@+id/date"
            style="@style/m4w_TextView.Subtitle"
            fontPath="fonts/InterstatePro-Light.otf"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/subtitle"
            android:maxLines="1"
            tools:text=" * date" />
    </LinearLayout>

</LinearLayout>

答案 3 :(得分:0)

尝试以下代码。您只需要先放置日期元素,然后再放置相对布局的副标题。

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

<TextView
                android:id="@+id/date"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:maxLines="1"
                tools:text=" * date" />

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:layout_toLeftOf="@+id/date"
                tools:text="subtitle" />


        </RelativeLayout>

答案 4 :(得分:0)

尝试此代码可能会对您有所帮助

  <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:layout_weight="1"
            tools:text="ajay bhimani this is practice"/>

        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/subtitle"
            android:maxLines="1"
            android:layout_weight="1"
            tools:text=" * date"/>
    </LinearLayout>

答案 5 :(得分:0)

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight = 1
                tools:text="subtitle" />

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight = 1
                tools:text=" * date" />
        </LinearLayout >

答案 6 :(得分:0)

尝试以下代码

 <LinearLayout
    android:layout_width="match_parent"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:layout_weight="1"
        android:ellipsize="end"
        tools:text="subtitle  df  ds fgfg cvb vbcfvnb cbxfcb" />

    <TextView
        android:id="@+id/date"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        tools:text=" * date" />
</LinearLayout>

答案 7 :(得分:0)

  

尝试将RelativeLayout替换为LinearLayout并为两个TextView添加layout_weight=1

更改为:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/subtitle"
        style="@style/m4w_TextView.Subtitle"
        fontPath="fonts/InterstatePro-Light.otf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1"
        tools:text="subtitle" />

    <TextView
        android:id="@+id/date"
        style="@style/m4w_TextView.Subtitle"
        fontPath="fonts/InterstatePro-Light.otf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_toRightOf="@+id/subtitle"
        android:maxLines="1"
        tools:text=" * date" />
</LinearLayout>

答案 8 :(得分:0)

要实现这两个目标,您需要在第二个TextView中的布局 wrap_content 中使用 wrap_content ,并在第一个TextView中使用 width:0dp和layout_weight:1的组合即可。这样,第二个TextView将始终足够大以显示内容,第一个TextView将扩展到它旁边的可用空间。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="this text is tool long so it will be cut. this text is tool long so it will be cut." />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:maxLines="1"
        android:text=" * date" />
</LinearLayout>

enter image description here