解决constraintlayout内的textview文本边距无效

一、前言:

我们在用约束布局的时候,经常遇到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是代表颜色整数的标记,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是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示。"
        />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。