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”
。谢谢!