Android RecyclerView 的 Item 中的 TextView 左重力问题 XML
Android TextView Left Gravity Problem in an Item XML for RecyclerView
我有一个 RecyclerViewAdapter,我正在准备一个项目 xml 以将我的所有项目显示为列表。
我的 xml 是这样写的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayoutAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bg_film_add_recycler_view">
<TextView
android:id="@+id/textViewFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textSize="16sp"
android:textColor="@color/figma_text_color_dark"
android:fontFamily="@font/inter500_regular"
app:layout_constraintBottom_toBottomOf="@id/imageViewFilmLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageViewFilmLogo"
app:layout_constraintTop_toTopOf="@id/imageViewFilmLogo"
tools:text="MyFilm">
</TextView>
<ImageView
android:id="@+id/imageViewFilmLogo"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:layout_marginVertical="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
因此,我看到我的 RecyclerView 是这样的(红色的是文本视图,绿色的是图像视图):
如您所见,文本视图看起来像是在特定区域居中。但我想像这样向左对齐:
我尝试添加 android:gravity="left"
但它不起作用,问题是什么?你能帮帮我吗?
如果可能的话,你能不能使“布局左边缘 - 图像视图”和“图像视图 - 文本视图左边缘”之间的距离相等
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayoutAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bg_film_add_recycler_view">
<ImageView
android:id="@+id/imageViewFilmLogo"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:layout_marginVertical="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ContentDescription">
</ImageView>
<LinearLayout
android:id="@+id/textViewFilmName_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@id/imageViewFilmLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageViewFilmLogo"
app:layout_constraintTop_toTopOf="@id/imageViewFilmLogo"
android:paddingStart="30dp">
<TextView
android:id="@+id/textViewFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textSize="16sp"
android:textColor="@color/figma_text_color_dark"
android:fontFamily="@font/inter500_regular"
tools:text="MyFilm">
</TextView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如果您希望在 constraintLayout 中设置像 layout_gravity="start"
这样的视图行为,请对视图使用 0dp
并且不要忘记为第二个元素添加边距开始
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayoutAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bg_film_add_recycler_view">
<TextView
android:id="@+id/textViewFilmName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:clickable="false"
android:textSize="16sp"
android:textColor="@color/figma_text_color_dark"
android:fontFamily="@font/inter500_regular"
app:layout_constraintBottom_toBottomOf="@id/imageViewFilmLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageViewFilmLogo"
app:layout_constraintTop_toTopOf="@id/imageViewFilmLogo"
tools:text="MyFilm">
</TextView>
<ImageView
android:id="@+id/imageViewFilmLogo"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:layout_marginVertical="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
我有一个 RecyclerViewAdapter,我正在准备一个项目 xml 以将我的所有项目显示为列表。
我的 xml 是这样写的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayoutAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bg_film_add_recycler_view">
<TextView
android:id="@+id/textViewFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textSize="16sp"
android:textColor="@color/figma_text_color_dark"
android:fontFamily="@font/inter500_regular"
app:layout_constraintBottom_toBottomOf="@id/imageViewFilmLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageViewFilmLogo"
app:layout_constraintTop_toTopOf="@id/imageViewFilmLogo"
tools:text="MyFilm">
</TextView>
<ImageView
android:id="@+id/imageViewFilmLogo"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:layout_marginVertical="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
因此,我看到我的 RecyclerView 是这样的(红色的是文本视图,绿色的是图像视图):
如您所见,文本视图看起来像是在特定区域居中。但我想像这样向左对齐:
我尝试添加 android:gravity="left"
但它不起作用,问题是什么?你能帮帮我吗?
如果可能的话,你能不能使“布局左边缘 - 图像视图”和“图像视图 - 文本视图左边缘”之间的距离相等
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayoutAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bg_film_add_recycler_view">
<ImageView
android:id="@+id/imageViewFilmLogo"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:layout_marginVertical="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ContentDescription">
</ImageView>
<LinearLayout
android:id="@+id/textViewFilmName_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@id/imageViewFilmLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageViewFilmLogo"
app:layout_constraintTop_toTopOf="@id/imageViewFilmLogo"
android:paddingStart="30dp">
<TextView
android:id="@+id/textViewFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textSize="16sp"
android:textColor="@color/figma_text_color_dark"
android:fontFamily="@font/inter500_regular"
tools:text="MyFilm">
</TextView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如果您希望在 constraintLayout 中设置像 layout_gravity="start"
这样的视图行为,请对视图使用 0dp
并且不要忘记为第二个元素添加边距开始
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayoutAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bg_film_add_recycler_view">
<TextView
android:id="@+id/textViewFilmName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:clickable="false"
android:textSize="16sp"
android:textColor="@color/figma_text_color_dark"
android:fontFamily="@font/inter500_regular"
app:layout_constraintBottom_toBottomOf="@id/imageViewFilmLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageViewFilmLogo"
app:layout_constraintTop_toTopOf="@id/imageViewFilmLogo"
tools:text="MyFilm">
</TextView>
<ImageView
android:id="@+id/imageViewFilmLogo"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:layout_marginVertical="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>