LoginSignup
1

More than 5 years have passed since last update.

ConstraintLayoutでTextViewのWidthをAnySizeにしたら文字が欠けてしまう問題(constraint-layout:1.0.0-alpha4)

Posted at

解決法

com.android.support.constraint:constraint-layout1.0.0-alpha5 以上のものを使う

build.gradle
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha5'

環境

  • AndroidStudio 2.2 Preview 6
  • com.android.tools.build:gradle:2.2.0-alpha6
  • com.android.support.constraint:constraint-layout:1.0.0-alpha4

問題

AndroidStudio 2.2 previewから新しく追加されたConstraintLayoutを使っていて、TextViewのWidthを中のTextによって可変 + 折り返して欲しかったのに文字が欠けてしまっていた

構成的にはこんな感じ

34e38678-c73e-e558-81cd-665ac530a3b2.png

layout.xml
<TextView
    android:text="hogehogehogehogehogehgoehogehgoehgoehgoeghoe"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:textSize="18sp"
    android:scrollHorizontally="true"
    app:layout_constraintLeft_toRightOf="@+id/imageView"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    android:layout_marginTop="8dp"
    app:layout_constraintRight_toRightOf="@+id/comment_item"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintBottom_toBottomOf="@+id/comment_item"
    android:layout_marginBottom="16dp"
    app:layout_constraintHorizontal_bias="0.0" />

これを実行すると、以下のように右端の文字が欠けてしまう

613f7c9c-6078-03cf-df12-5887d06b3b23.png

原因と解決法

com.android.support.constraint:constraint-layout:1.0.0-alpha4 のバグらしい
1.0.0-alpha5 で修正されたのでそちらを使う

現時点(2016/8/17)では 1.0.0-alpha7 が最新なのでそれを使ったら正しく表示された

build.gradle
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'

be779dbc-fd19-6153-b83e-6dd80b0e25dd.png

余談

ConstraintLayoutなどのAndroidSDK付属のtoolはgradleのdependenciesに書くだけではダウンロードされない
AndroidStudioのSDK Managerを開き、SDK Toolsタブで別途updateすると使えるようになる
ややこしいなこれ

0edba27d-e043-c04a-6eb3-5de3bd7f6b2f.png
6fcd44d6-90c9-399b-b1d8-e720a82a13d8.png

参考

http://stackoverflow.com/questions/38393954/wrong-textview-display-in-constraintlayout-when-anysize
http://stackoverflow.com/questions/37992187/gradle-sync-failed-could-not-find-constraint-layout1-0-0-alpha2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1