首页 > 解决方案 > 如何在 ImageView 旁边居中两个 TextView?

问题描述

我正在尝试实现以下输出:

在此处输入图像描述

但我得到的是:

在此处输入图像描述

我似乎无法弄清楚如何使两者居中TextViews。编码:

<?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:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/chat_room_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <!-- Progress bar -->
        <ProgressBar
            android:id="@+id/chat_room_image_progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxHeight="40dp"
            android:minHeight="40dp"
            android:minWidth="40dp"
            android:maxWidth="40dp"
            android:theme="@style/progressColor"
            android:layout_margin="15dp" />

        <!-- user's avatar image -->
        <ImageView
            android:id="@+id/chat_room_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/chat_room_title_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_toRightOf="@id/chat_room_image_layout"
        android:layout_toEndOf="@id/chat_room_image_layout">

        <!-- user names -->
        <TextView
            android:id="@+id/chat_room_users_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="@color/blackColor"
            android:includeFontPadding="false"
            android:layout_marginTop="10dp"/>

        <!-- last message -->
        <TextView
            android:id="@+id/chat_room_last_message_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="15sp"
            android:text="aaaaaaaaaaaaaaaaaaaaa"
            android:includeFontPadding="false"
            android:textColor="@color/blackColor"
            android:layout_marginBottom="10dp" />

    </LinearLayout>

</RelativeLayout>

化身的身高和体重是动态设置的50dp。我厌倦了关注这个话题,但没有任何成功。我怎样才能使这两个文本视图居中?

标签: androidandroid-layout

解决方案


您可以使用:

<LinearLayout
    android:layout_height="wrap_content"
    android:minHeight="56dp"
    android:layout_width="match_parent">

    <!-- user's avatar image -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingStart="15dp"
        android:paddingEnd="15dp"/>


    <!-- Progress bar -->
    <ProgressBar
        android:id="@+id/chat_room_image_progressbar"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ..>

    <LinearLayout
        android:id="@+id/chat_room_title_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <!-- user names -->
        <TextView
            android:id="@+id/chat_room_users_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            .../>

        <!-- last message -->
        <TextView
            android:id="@+id/chat_room_last_message_text"
            android:text="text2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            ... />

    </LinearLayout>
</LinearLayout>

在此处输入图像描述


推荐阅读