android - 对齐 TextView 基线和另一个 TextView 底部

标签 android textview alignment baseline

我有 RelativeLayout 和两个 TextView,其中一个有背景。

在图像上两条基线对齐,但我想对齐一个的基线和另一个的背景底部。

enter image description here

这是我的 xml 代码。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/chat_room_item_height"
    android:background="?attr/selectableItemBackground"
    android:paddingEnd="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingStart="16dp"
    >

    <!-- other stuff -->

    <TextView
        android:id="@+id/text_view_unread_message_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/text_view_last_message_date"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/chat_unread_message_count_background"
        style="@style/ChatRoomUnreadMessageCountTextStyle"
        tools:text="101"
        />

  <TextView
      android:id="@+id/text_view_last_message"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/text_view_title"
      android:layout_toEndOf="@id/image_view_icon"
      android:layout_toLeftOf="@id/text_view_unread_message_count"
      android:layout_toRightOf="@id/image_view_icon"
      android:layout_toStartOf="@id/text_view_unread_message_count"
      android:ellipsize="end"
      android:layout_alignBaseline="@+id/text_view_unread_message_count"
      android:gravity="bottom"
      android:maxLines="1"
      tools:text="My life is a story book with many different chapters but I don’t let everyone read it."
      style="@style/ChatRoomLastMessageTextStyle"
      />

</RelativeLayout>

我试过将带有背景的 TextView 包装到不同的 ViewGrop,将两个 TextView 包装到布局,但无济于事。

最佳答案

你可以玩点小把戏,我们开始吧:

text_view_unread_message_count 放在某个 View 组中,例如RelativeLayout 并使用 SP 缩放添加一个 paddingBottom(这是您手动对齐的空间),如下所示:

Obs.: 你需要给你的 TextView 一个新的 ID,这是一个 ID 和大小值可以改变的例子。

   <!-- sp scaling will change with font size -->
    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingBottom="10sp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_toEndOf="@id/image_view_icon"
        android:layout_toLeftOf="@id/text_view_unread_message_count"
        android:layout_toRightOf="@id/image_view_icon"
        android:layout_toStartOf="@id/text_view_unread_message_count">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/chat_unread_message_count_background"
                style="@style/ChatRoomUnreadMessageCountTextStyle"
                tools:text="101"
        />
    </RelativeLayout>

将位置定义放在父级上,将文本定义放在 TextView 上。这应该有效。

关于android - 对齐 TextView 基线和另一个 TextView 底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39892670/

相关文章:

android - annotationProcessor + androidTest + dagger2

android - Android TextView 中的重叠问题

struct - Vala 中的字节对齐

android - 如何从 URI 获取完整的文件路径

android - Flutter:如何在 Flutter 中使图像的某些区域透明?

安卓工作室 3.1.3 错误 : Unable to resolve dependency

android - getText 不可访问

android - 在 Android 应用程序中设置文本大小

css - 对齐 3 个 div,其中一个 div 具有固定宽度

CSS 对齐 : horizontal blocking?