首页 > 解决方案 > TextView 忽略左右边距

问题描述

我的布局包括:

<?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 左右边距。

标签: androidlayouttextview

解决方案


Layout Margin 影响 View 对象,在本例中为 TextView。不是对象内的实际文本。所以它仍然会紧贴物体的边缘。我建议使用填充,它会直接影响文本。您可能还想考虑向对象添加重力?

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

android:paddingStart="16dp"

干杯!


推荐阅读