对齐两个TextViews,一个人留下的其他权利,在一个ListView无拉伸背景权利、背景、两个、个人

2023-09-12 21:26:44 作者:傲娇小仙女

所以,我有两个 TextViews 每行中的的ListView 。每个人都应该留给另一个右对齐。无论 TextViews 拥有圆角矩形作为背景,应该只是包装内的文本。于是我想出了这一点:

So I've got two TextViews per row in a ListView. One should be left and the other right aligned. Both TextViews have a rounded rectangle as background which should just wrap the text inside. So I came up with this:

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

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/bubble_purple"
        android:gravity="center" >
    </TextView>

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/text_right"
        android:background="@drawable/bubble_blue"
        android:gravity="center" >
    </TextView>
</RelativeLayout>

看起来长文好,但不适合短消息:

It looks good for long texts but not for shorter messages:

我也尝试了LinearLayout中是这样的:

I also tried a LinearLayout like this:

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

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/bubble_blue"
        android:gravity="center" >
    </TextView>

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

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/bubble_purple"
        android:gravity="center">
    </TextView>
</LinearLayout>

这适用于短消息而不是更长的:

This works for short messages but not for longer ones:

是它在某种程度上可以测量 TextViews 的组合宽度和这些布局的程序中切换,还是我做错了什么吗?

Is it somehow possible to measure the combined width of the TextViews and programmatically switch between these layouts, or am I doing something wrong here?

推荐答案

这是否工作,你想要什么?

Does this work as you want?

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

<TextView
    android:id="@+id/text_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:background="@drawable/bubble_blue"
    android:text="2"
    android:gravity="center" >
</TextView>

<TextView
    android:id="@+id/text_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:background="@drawable/bubble_purple"
    android:text="9"
    android:gravity="center" >
</TextView>
</LinearLayout>

通过添加layout_weight参数,使他们平等的,你告诉布局引擎平等分配任何空余的房间的两种观点之间。如果你想左块比右边大,你可以给它一个较大的权重。

By adding the layout_weight parameters and making them equal, you tell the layout engine to allocate any spare room equally between the two views. If you wish the left block to be larger than the right, you can give it a larger weight.

编辑:这也许是更好的:

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

<LinearLayout 
    android:id="@+id/text_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#456"
        android:text="5 + 1"
        android:gravity="center" >
    </TextView> 

</LinearLayout>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=" = " >
</TextView>

<LinearLayout 
    android:id="@+id/text_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:layout_weight="0.5">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#856"
            android:text="6"
            android:gravity="center" >
        </TextView>

</LinearLayout>

</LinearLayout>