gpt4 book ai didi

android - 在 RelativeLayout 中正确对齐 TextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:16 25 4
gpt4 key购买 nike

我正在为一门大学类(class)开发一个即时消息应用程序,类似于 Whatsapp,您可以在所附屏幕截图中看到该图标:)。

我有一个用自定义 CursorAdapter 填充的 ListFragment。它提供两种类型的 View ,发送消息(右对齐)和接收消息(左对齐)。

发送消息的布局很好,但我不能说接收消息的布局也一样。

截图如下:

enter image description here

发送消息布局:

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

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FFCCCCCC"
android:padding="5dp" >

<TextView
android:id="@+id/ceo_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFBBBBBB"
android:gravity="center"
android:textSize="10sp" />

<TextView
android:id="@+id/ceo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ceo_timestamp"
android:background="#FFAAAAAA"
android:gravity="right"
android:textSize="16sp" />
</RelativeLayout>

</RelativeLayout>

收到的消息布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FF999999"
android:padding="5dp" >

<TextView
android:id="@+id/cei_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FF888888"
android:gravity="center"
android:textSize="10sp" />

<TextView
android:id="@+id/cei_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/cei_timestamp"
android:background="#FF777777"
android:gravity="right"
android:textSize="16sp" />
</RelativeLayout>

</RelativeLayout>

添加那些丑陋的背景颜色只是为了查看每个 View 和布局占用的空间。正如您在屏幕截图中看到的,发送消息(右对齐)效果很好。收到的消息(左对齐)没有很好地对齐,因为 RelativeLayout 占用了所有可用的水平空间,而不是仅仅包装内容。

有谁知道如何修复它,或者更好的布局设计来完成我的需要?

非常感谢。

最佳答案

在已发送消息布局中,将 android:layout_alignParentRight="true"参数替换为 android:layout_alignParentLeft="true"。

关于android - 在 RelativeLayout 中正确对齐 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com