本文介绍了在 Android 上将一个 TextView 放在另一个 TextView 旁边以获得更长的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的目标是让 2 个 TextView (textLeft, textRight) 相邻.textLeft 应该与父级的左侧对齐.textRight 应该始终是 textLeft 的直接右侧(而不是在设备的最右侧),并且如果 textLeft 不应该被推出屏幕> 太长了.

My goal is to have 2 TextViews (textLeft, textRight) next to each other. textLeft should be aligned to the left side of parent. textRight should always be an immediate right of textLeft (not on the extreme right of device), and it should not be pushed out of screen if textLeft is too long.

水平 LinearLayout 在这里没有用,因为使用权重会固定视图大小,这是不希望的.

Horizontal LinearLayout is of no use here as using weights will fix the view size, and that's not desired.

我一直在使用这个 RelativeLayout 如果 textLeft 不是太长,它工作正常.对于较长的文本,textRight 会被推出屏幕.

I have been using this RelativeLayout which works fine if textLeft is not too long. With longer texts, textRight is pushed off screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TextAlignActivity">

    <TextView
            android:id="@+id/textLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:maxLines="3"
            android:ellipsize="end"
            android:text="Text On the Left - long random text to follow"
    />

    <TextView
            android:id="@+id/textRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/textLeft"
            android:layout_alignParentRight="true"
            android:textSize="10sp"
            android:maxLines="1"
            android:text="Text on the Right"
    />

</RelativeLayout>

我想 ConstraintLayout 可能很适合这种情况,但我不是很熟悉.如果可能的话,我想避免这种情况,因为它需要对我现有的布局进行大量重写.

I guess ConstraintLayout may be a good fit for such cases, but I am not very familiar. I would like to avoid that if possible, since it will need a heavy re-write of my existing layout.

  1. 为了简化我的查询,我在示例中使用了 2 个 TextView .在我的应用代码中,textLeft 是一个垂直的 LinearLayout,带有 2 个 TextViews(textLeftToptextLeftBottom>) 而不是示例中所示的单个 TextView.我希望适用于 2 TextViews 的解决方案也适用于 LinearLayoutTextView.

  1. In order to simplify my query, I am using 2 TextViews in the example. In my app code textLeft is a vertical LinearLayout with 2 TextViews (textLeftTop and textLeftBottom) instead of a single TextView as shown in example. I am hoping the solution that works for 2 TextViews should also work for a LinearLayout and TextView.

根据目前的解决方案,我应该澄清一下,如果 textRight 位于 textLeft 的右侧(不是设备右侧)>textLeft 不长.如果 textLeft 很长,那么 textRight 应该粘"到设备右侧,并且 textLeft 应该包装文本.

Based on the solutions so far, I should clarify that I need the textRight to be on immediate right of textLeft (not device right) if textLeft is not long. If textLeft is long, then textRight should "stick" to the device right, and textLeft should wrap text.

推荐答案

这种布局是一种聊天"布局.您可以使用两个 LinearLayout,一个是 match_parent 的父级,另一个是两个 TextView 的持有者.请注意,后一个具有 wrap_content 宽度.

This layout is a kind of "chatting" layout. You can use two LinearLayout, one is parent of all with match_parent, and the other is holder of two TextViews. Note that latter one has wrap_content width.

layout_weight 属性意味着......粗略地说,较小的数字很重要.

layout_weight attribute of LinearLayout means... roughly speaking, smaller number is important.

另见:https://developer.android.com/reference/android/小部件/线性布局

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

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

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test test test test test test test test test test test test test test test"
                android:layout_weight="1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello world"
                android:layout_weight="0"/>
    </LinearLayout>

    <Space android:layout_width="wrap_content" android:layout_height="20dp"/>

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

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test test test test test test test test "
                android:layout_weight="1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello world"
                android:layout_weight="0"/>
    </LinearLayout>

</LinearLayout>

过时的答案

注意 textLeft 的宽度为 0dp.

Note that textLeft has 0dp width.

如果在约束布局中将宽度(或高度)设置为 0,则意味着它将填充其余的空白空间.

If you set width (or height) to 0 in constraint layout, it means it will fill rest of the empty space.

或者,您可以使用 app:layout_constraintHorizo​​ntal_weight 和 0dp 宽度,您可以设置布局宽度的百分比.

Or, you can use app:layout_constraintHorizontal_weight with 0dp width, you can set percentage of width of layout.

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:id="@+id/textLeft"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Long Long Long Long Long Long Long Long Long Long Long Long Long"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/textRight"
            app:layout_constraintTop_toTopOf="parent"/>

    <TextView
            android:id="@+id/textRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!  "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/textLeft"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

这篇关于在 Android 上将一个 TextView 放在另一个 TextView 旁边以获得更长的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 19:00