我目前正在回收器中使用卡片视图,尽管我付出了努力,但文本仍是左对齐的。
这是图书项目布局。
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/bookTitleTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
这是回收站本身。
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bookRecycler" />
这是当前的输出。在此处输入图片说明
如您所见,文本未居中。
我尝试使用布局重力将无效的文本居中。我还尝试在文本视图上使用约束布局并使用约束使文本居中,但没有任何运气。
android:layout_gravity = "center" 已经尝试过了。
关于我所忽视的任何建议都会很棒。谢谢你。
添加android:gravity="center"
为LinearLayout
,你很高兴去
我看到你LinearLayout
只有一个孩子并且没有特殊属性,所以你甚至可以直接删除整个LinearLayout
地方,其中. 为了使孩子居中,您也可以使用父母和/或孩子TextView
CardView
extends FrameLayout
FrameLayout
android:gravity="center"
android:layout_gravity="center"
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@color/white">
<TextView
android:layout_gravity="center"
... rest of code
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句