android - 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 左右边距。
解决方案
Layout Margin 影响 View 对象,在本例中为 TextView。不是对象内的实际文本。所以它仍然会紧贴物体的边缘。我建议使用填充,它会直接影响文本。您可能还想考虑向对象添加重力?
我还建议在这些视图上使用布局权重,否则您将遇到调整大小的问题!
android:paddingStart="16dp"
干杯!
推荐阅读
- firebase - 如何在 Firebase 和 Visual Studio 之间连接?
- ios - 在 Swift 中合并两个结构数组
- python - 使用 python 查找昨天在 linux 主机上创建的所有“*.wav”文件
- c++ - 无法让 cmake 链接到 GLFW 库
- java - Processbuilder 抛出 IO 异常
- linux - 使用 linux 命令在匹配行的末尾添加带有目录结构的字符串
- docker - 如何在不在 docker.com 上创建帐户并登录的情况下下载 Docker for Windows?
- generics - 在 Kotlin 列表中使用泛型时出现类型不匹配错误
- python - django 在生产环境中,urls.py 仅匹配空路径,即“”,仅此而已?
- unix - 如何避免在excel中将最后三位数字转换为零