markdown TextView不能在ConstraintLayout中使用wrap_content

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown TextView不能在ConstraintLayout中使用wrap_content相关的知识,希望对你有一定的参考价值。

# `TextView` can't `wrap_content` in `ConstraintLayout`

## New Solution, need ConstraintLayout 1.1.0

[SOURCE](https://android.jlelse.eu/whats-new-in-constraint-layout-1-1-0-acfe30cfc7be)

This behaviour has been fixed in version 1.1.0. Now `TextView` will grow if the text is too long but still respect the constraint:

![Image](https://cdn-images-1.medium.com/max/1600/1*ify5Rpw70YY1NdQmv2rPfw.jpeg)

We will need either `app:layout_constrainedWidth=”true”` or `app:layout_constrainedHeight=”true”`

```xml
<TextView
    android:layout_width="wrap_content" <----
    android:layout_height="wrap_content"
    android:text="Hello this is an example with constraint width"
    app:layout_constrainedWidth="true"  <----
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="@+id/guideline_50"/>
```


## Old Solution, use the solution above instead

[SOURCE](https://stackoverflow.com/q/46064963), [SOURCE](https://stackoverflow.com/a/40881429)

`TextView` doesn't seem to respect `wrap_content` inside `ConstraintLayout` (long text get cut off).

Solution, change with to 0dp:

```
android:layout_width="0dp"
app:layout_constraintWidth_default="wrap" //optional, seem to work fine without this in ConstraintLayout 1.0.2
```

以上是关于markdown TextView不能在ConstraintLayout中使用wrap_content的主要内容,如果未能解决你的问题,请参考以下文章

markdown Android TextView字体加粗

markdown TextView超出限制自动添加省略号

markdown Android的开发之TextView的中间设置横线,适用于电商项目,商品原价之类的功能。

markdown const멤버함수

markdown const,volatile

markdown 2つの的TextViewを左寄せで并べて表示するとき,左のTextView的が长くなっても右の的TextViewは必ず表示する(线路のメンバーと人数みたいな)