Android 在布局中并排放置两个文本视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21778632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:03:40  来源:igfitidea点击:

Put two textviews side by side in a layout

androidtextview

提问by Esteam

I have two textviews i need to put side by side in a layout and I have to respect two rules:

我有两个文本视图需要在布局中并排放置,我必须遵守两个规则:

  • Textview2 needs always to be displayed entirely.

  • Textview1 has to be cropped if there is no enough room in the layout.

  • Textview2 需要始终完整显示。

  • 如果布局中没有足够的空间,则必须裁剪 Textview1。

Examples:

例子:

Textview1|textview2

文本视图1|文本视图2

Teeeeeeeeeeeeeeeeeeeextview1...|textview2

Teeeeeeeeeeeeeeeeeeeeeextview1...|textview2

Any ideas?

有任何想法吗?

The only way I found that might work is to create a drawable with the text of textview2 and affect it as coumpoundDrawable to textview1.

我发现可能有效的唯一方法是使用 textview2 的文本创建一个 drawable,并将其作为 coumpoundDrawable 影响到 textview1。

回答by Dean

Wrap the two TextViews in a LinearLayout. Assign a layout weight of 0 to textview2 and a layout weight of 1 to textview2.

将两个 TextView 包装在一个 LinearLayout 中。为 textview2 分配 0 的布局权重,为 textview2 分配 1 的布局权重。

See here for more info: Linear Layout Weight

有关更多信息,请参见此处:线性布局权重

If you play with the example below you'll see that the LinearLayout first allocates space to textview2 (with weight 0) and then allocates whatever remains to textview1 (with weight 1). If there is insufficient space to accommodate both TextViews, textview1 will be ellipsized first. In the example below textview2 will only ever become ellipsized if the LinearLayout is smaller than the size of textview2 itself. Assign a specific layout width to the FrameLayout and see what happens.

如果您使用下面的示例,您将看到 LinearLayout 首先将空间分配给 textview2(权重为 0),然后将剩余的空间分配给 textview1(权重为 1)。如果没有足够的空间容纳两个 TextView,则 textview1 将首先被椭圆化。在下面的示例中,只有当 LinearLayout 小于 textview2 本身的大小时,textview2 才会变成椭圆。为 FrameLayout 分配一个特定的布局宽度,看看会发生什么。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="match_parent" 
    android:background="#FF0000FF">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFF0000"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="textview1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:background="#FF00FF00"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="textview2" />
    </LinearLayout>

</FrameLayout>

回答by Pararth

why do you need that drawable? option: you can use textview1 with a fixed width and ellipsize the end..:

你为什么需要那个drawable?选项:您可以使用具有固定宽度的 textview1 并将结尾椭圆化..:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:padding="2dp" />


    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textview1"
        android:padding="2dp" />

</RelativeLayout>  

回答by Devrim

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

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_light"
        android:singleLine="true"
        android:text="textView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:singleLine="true"
        android:text="textView2" />

</LinearLayout>

Edit:

编辑:

Output for short textView1 text:

短 textView1 文本的输出:

enter image description here

在此处输入图片说明

Output for long textView1 text:

长 textView1 文本的输出:

enter image description here

在此处输入图片说明

回答by Ak.Ha

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight=".50"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight=".50"/>

    </LinearLayout>