如何在recyclerview的cardview中对齐textview

问题描述 投票:0回答:3

我正在使用数据库中的数据创建一个

RecyclerView
。为了制作一行,我创建了一个资源文件作为
row_layout.xml
,它有两个
TextView
。我想要它在
CardView
中,但在创建
TextView
时创建的无法对齐。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_gravity="center">
        <TextView
            android:id="@+id/sl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textColor="#000"
            android:textSize="40sp"
            android:textStyle="bold"
            tools:ignore="MissingConstraints"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textColor="#000"
            android:textSize="40sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="@id/sl"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteY="9dp" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

android xml android-layout
3个回答
1
投票

您只需以正确的方式使用

Constraints
,而不是通过添加
tools:ignore="MissingConstraints"
来抑制警告。

  1. 左对齐时:

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp">
    
            <TextView
                android:id="@+id/sl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textColor="#000"
                android:textSize="40sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/nameTV"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/nameTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textColor="#000"
                android:textSize="40sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toRightOf="@id/sl"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
    
  2. 当与父级的左侧和右侧对齐时,使它们的距离相等:

     <androidx.cardview.widget.CardView
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    
         <androidx.constraintlayout.widget.ConstraintLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:padding="12dp">
    
             <TextView
             android:id="@+id/sl"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="1"
             android:textColor="#000"
             android:textSize="40sp"
             android:textStyle="bold"
             app:layout_constraintLeft_toLeftOf="parent"
             app:layout_constraintRight_toLeftOf="@+id/nameTV"
             app:layout_constraintTop_toTopOf="parent" />
    
         <TextView
             android:id="@+id/nameTV"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Name"
             android:textColor="#000"
             android:textSize="40sp"
             android:textStyle="bold"
             app:layout_constraintRight_toRightOf="parent"
             app:layout_constraintLeft_toRightOf="@id/sl"
             app:layout_constraintTop_toTopOf="parent" />
         </androidx.constraintlayout.widget.ConstraintLayout>
     </androidx.cardview.widget.CardView>
    
  3. 当与父级的左侧和右侧对齐但采用水平链样式打包时,就像内部展开一样,会将它们展开到两端:

     <androidx.cardview.widget.CardView
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    
         <androidx.constraintlayout.widget.ConstraintLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:padding="12dp">
    
             <TextView
             android:id="@+id/sl"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="1"
             android:textColor="#000"
             android:textSize="40sp"
             android:textStyle="bold"
             app:layout_constraintHorizontal_chainStyle="packed"
             app:layout_constraintLeft_toLeftOf="parent"
             app:layout_constraintRight_toLeftOf="@+id/nameTV"
             app:layout_constraintTop_toTopOf="parent" />
    
         <TextView
             android:id="@+id/nameTV"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Name"
             android:textColor="#000"
             android:textSize="40sp"
             android:textStyle="bold"
             app:layout_constraintRight_toRightOf="parent"
             app:layout_constraintLeft_toRightOf="@id/sl"
             app:layout_constraintTop_toTopOf="parent" />
         </androidx.constraintlayout.widget.ConstraintLayout>
     </androidx.cardview.widget.CardView>
    
  4. 当与父级的右侧对齐时:

     <androidx.cardview.widget.CardView
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    
         <androidx.constraintlayout.widget.ConstraintLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:padding="12dp">
    
             <TextView
             android:id="@+id/sl"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="1"
             android:textColor="#000"
             android:textSize="40sp"
             android:textStyle="bold"
             app:layout_constraintHorizontal_chainStyle="packed"
             app:layout_constraintLeft_toLeftOf="parent"
             app:layout_constraintRight_toLeftOf="@+id/nameTV"
             app:layout_constraintTop_toTopOf="parent" />
    
         <TextView
             android:id="@+id/nameTV"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Name"
             android:textColor="#000"
             android:textSize="40sp"
             android:textStyle="bold"
             app:layout_constraintRight_toRightOf="parent"
             app:layout_constraintLeft_toRightOf="@id/sl"
             app:layout_constraintTop_toTopOf="parent" />
         </androidx.constraintlayout.widget.ConstraintLayout>
     </androidx.cardview.widget.CardView>
    

就像这些一样,使用开始/左边距和结束/右边距以及布局宽度和高度,并根据需要创建自定义布局。我使用了“左”和“右”而不是“开始”和“结束”,但如果您想让应用程序支持 RTL(从右到左)布局,请仅使用“开始/结束”约束和边距。 另外,如果你设置视图的左右约束,然后将宽度设置为

0dp
,它将完全可用空间。
ConstraintLayout
非常先进,看看官方文档


0
投票

尝试使用这个

   <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/s1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="1"
            android:textColor="#000"
            android:textSize="40sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/s2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/s2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Name"
            android:textColor="#000"
            android:textSize="40sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/s1"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

希望这有帮助...请随时要求澄清...


0
投票

对我来说最简单的方法是在我的cardView中使用相对布局,然后使用“android:layout_centerHorizontal =“true”/或任何中心类型属性都可以正常工作。当相对布局的宽度和高度时,这非常有效是匹配父级(父级是cardView)。

请告诉我这是否有用

© www.soinside.com 2019 - 2024. All rights reserved.