我在下圖中選擇的 TextView 有問題,我使用 ConstraintLayout。

在為多種語言放置一些字串資源檔案后,我遇到了 TextView 與其他視圖重疊的情況,因為新字串太長并且我使用了 wrap_content 的寬度。所以我從 wrap_content 更改為 match_constraint 并且 TextView 在多行上換行,這很好,但是在字串很短的情況下,附加到 TextView 左側的可繪制物件離文本太遠了。那是因為我使用的是重心,它在文本和可繪制物件之間留下了很大的空白間隙。
在不影響多行字串的情況下,我似乎找不到解決方案。可以對每種極端情況語言進行硬編碼,現在應該不成問題,但是當我擴展到更多語言時,它并不是理想的解決方案。
試圖通過代碼而不是xml來解決它,如果TextView的lineCount為1,那么我需要使用wrap_content而不是match_parent,因為它沒有重疊,但我似乎并沒有一致地作業,因為視圖繪圖未完成,螢屏旋轉有問題。
這就是我要的:

這就是我得到的:

這是我擁有的 xml,如果它有幫助,我不介意將它分成多個組件,但到目前為止,一個帶有 drawableLeft 的 TextView 就足夠了。謝謝你。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/header_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@ id/back_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:drawablePadding="10dp"
android:textColor="#D3DDDD"
android:textSize="14sp"
android:visibility="invisible"
app:drawableStartCompat="@drawable/left_arrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/refresh_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:drawablePadding="8dp"
android:gravity="center"
android:text="@string/pull_down_to_refresh"
android:textColor="@color/white"
android:textSize="14sp"
app:drawableStartCompat="@drawable/refresh"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@ id/legend"
app:layout_constraintStart_toEndOf="@ id/back_text"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@ id/rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:src="@drawable/rotate_device"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@ id/legend"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@ id/rotation"
android:src="@drawable/legend_inactive"
app:layout_constraintBottom_toBottomOf="@ id/rotation"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toStartOf="@ id/rotation"
app:layout_constraintTop_toTopOf="@ id/rotation" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
對于您的用例,您可以使用它,MaterialButton因為它icon在您的案例中提供 & 非常重要iconGravity=textStart。
所以你的布局看起來像這樣:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/header_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@ id/back_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:drawablePadding="10dp"
android:textColor="#D3DDDD"
android:textSize="14sp"
android:visibility="invisible"
app:drawableStartCompat="@drawable/left_arrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@ id/refresh_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:drawablePadding="8dp"
android:gravity="center"
android:text="Pull to refreshmckdmcdkmckdsmckdsmckdcm"
android:textColor="@color/white"
android:textSize="14sp"
android:background="@android:color/transparent"
app:icon="@drawable/refresh"
app:iconGravity="textStart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@ id/legend"
app:layout_constraintStart_toEndOf="@ id/back_text"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@ id/rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:src="@drawable/rotate_device"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@ id/legend"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@ id/rotation"
android:src="@drawable/legend_inactive"
app:layout_constraintBottom_toBottomOf="@ id/rotation"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toStartOf="@ id/rotation"
app:layout_constraintTop_toTopOf="@ id/rotation" />
</androidx.constraintlayout.widget.ConstraintLayout>
長文本輸出:

短文本輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/426330.html
