文本视图忽略左边距和右边距



我的布局包括:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnFindPOIs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Find POIs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGetLocation" />
<TextView
android:id="@+id/txtPoiDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnFindPOIs" />
</androidx.constraintlayout.widget.ConstraintLayout>

当 TextView 的内容超出一行时,它会正确流向第二行,但文本会向右延伸到屏幕边缘,忽略 16dp 左右边距。

布局边距会影响视图对象,在本例中为 TextView。不是对象内的实际文本。所以它仍然会直接碰到物体的边缘。 我建议使用填充,它会直接影响文本。您可能还想考虑为物体增加重力?

我还建议在这些视图上使用布局权重,否则您将遇到调整大小的问题!

android:paddingStart="16dp"

干杯!

最新更新