解決constraintlayout內的textview文本邊距無效

一、前言:

我們在用約束布局的時候,經(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個的顏色表示。"
        />
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容