我有一個 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 是這樣的(紅色的是文本視圖,綠色的是影像視圖):

如您所見,textviews 看起來像在特定區域居中。但我想像這樣左對齊:

我嘗試添加android:gravity="left"但它不起作用,是什么問題?你能幫我嗎?
如果可能,您能否平衡“布局左邊緣 - imageview”和“imageview - textview 左邊緣”之間的距離
uj5u.com熱心網友回復:
0dp如果您希望像layout_gravity="start"在約束布局中一樣設定視圖的行為,請用于視圖并且不要忘記為第二個元素添加邊距開始
<?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>
uj5u.com熱心網友回復:
<?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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/353702.html
標籤:安卓 xml 安卓布局 文本视图 android-constraintlayout
上一篇:SharpSerializer:忽略反序列化的屬性/屬性
下一篇:Delphi記錄賦值
