一、前言:
我們在用約束布局的時候,經(jīng)常遇到text設置文字沾滿一行,左右邊距無效的情況。

問題示例.png
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="注意:這種方式0x是代表顏色整數(shù)的標記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個的顏色表示,不接受ff00ff這種6個的顏色表示。"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
二、解決問題:
1 、方式一:
修改TextView寬度為match_parent
android:layout_width="match_parent"
2 、方式二:
修改TextView寬度為0dp
android:layout_width="0dp"
2 、方式三:
給TextView添加 app:layout_constrainedWidth="true",(不寫默認即false)
app:layout_constrainedWidth="true"
android:layout_width="wrap_content"
效果圖如下:

正常示例.png
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="注意:這種方式0x是代表顏色整數(shù)的標記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個的顏色表示,不接受ff00ff這種6個的顏色表示。"
/>