Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在列表视图上对齐两个文本视图,一个左一个右,而不拉伸背景_Android_Android Layout_Android Listview_Alignment_Textview - Fatal编程技术网

Android 在列表视图上对齐两个文本视图,一个左一个右,而不拉伸背景

Android 在列表视图上对齐两个文本视图,一个左一个右,而不拉伸背景,android,android-layout,android-listview,alignment,textview,Android,Android Layout,Android Listview,Alignment,Textview,因此,在列表视图中,每行有两个文本视图。一个应该左对齐,另一个应该右对齐。两个textview都有一个圆角矩形作为背景,应该只将文本包裹在里面。所以我想到了这个: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" >

因此,在
列表视图中,每行有两个
文本视图。一个应该左对齐,另一个应该右对齐。两个
textview
都有一个圆角矩形作为背景,应该只将文本包裹在里面。所以我想到了这个:

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

它适用于长文本,但不适用于短消息:

我还尝试过这样的线性布局:

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

这适用于短消息,但不适用于长消息:


是否有可能测量
文本视图的组合宽度并以编程方式在这些布局之间切换,或者我在这里做错了什么?

这是否可以按照您的意愿工作

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

通过添加布局权重参数并使其相等,可以告诉布局引擎在两个视图之间平均分配任何空闲空间。如果希望左侧块比右侧块大,可以给它更大的权重

编辑:也许这样更好:

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


尝试android:layout_weight=“1”两种方法如果第一个小而第二个大,两个
文本视图应该如何伸展?如果第二个
TextView
大,但它们的组合宽度小于
列表视图
,则应该看起来更像。或者,如果它们的组合宽度大于列表宽度,则是请求的行为。不,不完全是。当然,这对长文本有效,但对短文本有效。理想情况下,背景应该只包裹文本,短消息的气泡之间应该有空间,如上面的第二张图片。很好!将它们包装在
视图组中
,从未想过这一点。然而,我得到了一个“找不到资源标识符”的
android:layoutDirection
。但是,将第二个内部
LinearLayout
切换到
FrameLayout
,并添加了一个
android:layout\u gravity=“right”
。谢谢!