![](/img/trans.png)
[英]How can I implement a RecyclerView and a CardView into a fragment?
[英]How can I align the textview in cardview for recyclerview
我正在使用数据库中的数据创建一个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>
您只需要以正确的方式使用Constraints
,而不是通过添加tools:ignore="MissingConstraints"
来抑制警告。
左对齐时:
<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>
当与父母的左右对齐时,这使得它们的距离相等:
<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>
当与父级的左右对齐但水平链样式打包时,就像这样在内部展开时,它们会在两端展开:
<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>
当与父级的右侧对齐时:
<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>
就像这些一样,使用 Start/Left 和 End/Right Margins 以及布局宽度和高度,并根据需要创建自定义布局。 我使用了 Left 和 Right 而不是 Start 和 End,但是如果您想让您的应用程序支持 RTL(从右到左)布局,请仅使用 Start/End 约束和边距。 此外,如果您同时设置视图的左右约束,然后将宽度设置为0dp
,它将完全可用空间。 ConstraintLayout
非常高级,请查看官方文档。
尝试使用这个
<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>
希望这会有所帮助...随时要求澄清...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.