【问题标题】:How to align text and image in cardview?如何在cardview中对齐文本和图像?
【发布时间】:2023-04-09 18:19:01
【问题描述】:

我设计了一个布局,其中数据将来自 recyclerview。我面临的问题是我无法对齐项目。

下面是sn-p图片。

这是我的布局

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

    <androidx.cardview.widget.CardView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:background="@color/white"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="40dp">

            <TextView
                android:gravity="center"
                android:text="prnce"
                android:textColor="@color/black"
                android:backgroundTint="@color/white"
                android:id="@+id/groupname"
                android:layout_weight="1"
                android:textAlignment="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

            </TextView>
            <TextView
                android:gravity="center"
                android:text="1"
                android:textColor="@color/black"
                android:backgroundTint="@color/white"
                android:id="@+id/groupCount"
                android:textAlignment="center"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </TextView>
            <ImageButton

                android:backgroundTint="@color/white"
                android:src="@drawable/myhomegrey"
                android:id="@+id/homeWindow"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

            </ImageButton>

            <ImageButton
                android:backgroundTint="@color/white"
                android:src="@drawable/stargrey"
                android:id="@+id/favorite"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

            </ImageButton>

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</RelativeLayout>

如何将每个项目对齐在中心,以便它们可以排成一行,这样如果 textview 文本大小增加或减少,它不应该影响其他布局的对齐方式

【问题讨论】:

    标签: java android-studio android-linearlayout cardview


    【解决方案1】:

    您分配的权重导致多余的空间在小部件之间均匀分布。为文本字段“groupName”指定权重并删除其他字段的权重。这会将所有多余的空间分配给“groupName”文本视图。这也会将所有其他小部件推到右侧。您可以分配边距或填充以将这些小部件隔开。

    您可能还需要考虑为文本视图“groupCount”分配特定大小。

    另一种方法是使用 ConstraintLayout

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 2021-08-24
      • 2016-04-13
      • 2016-04-06
      • 1970-01-01
      相关资源
      最近更新 更多