问题描述
我设计了一个布局,其中数据将来自 recyclerview。我面临的问题是我无法对齐项目。
下面是截图。
这是我的布局
<?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 文本大小增加或减少,它不应该影响其他布局的对齐
解决方法
您分配的权重导致多余的空间在小部件之间均匀分布。为文本字段“groupName”指定权重并删除其他字段的权重。这会将所有多余的空间分配给“groupName”文本视图。这也会将所有其他小部件推到右侧。您可以指定边距或内边距来将这些小部件隔开。
您可能还需要考虑为文本视图“groupCount”分配特定大小。
另一种方法是使用 ConstraintLayout。