Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

制約を守らず、文字が溢れてしまうとき

Posted at

▼問題

ScrollViewのheightをwrapcontentにしたところ制約を守らず、文字が溢れてしまった。

・理想

スクリーンショット (14)s.png
↓スクロールすると
スクリーンショット (10).png

・今回

スクリーンショット (11).png
(ScrollViewそのもの自体が伸びて、ほかのViewに覆い被さっています)
一応コードはこんな感じです

<androidx.constraintlayout.widget.ConstraintLayout ~省略~>
    <TextView
        android:id="@+id/view_1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:gravity="center|bottom"
        android:text="↓ここまでViewがある"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view_2"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:gravity="center|top"
        android:text="↑ここまでViewがある"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toTopOf="@id/view_2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="top\n1\n2\n3\n4\nbottom" />
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

実際は、制約はちゃんとついているのですが、wrap_contentが優先されて
あたかも制約を守っていないように見えています。

▼解決

ScrollViewのattributeに「layout_constrainedHeight="true"」を追加する。

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_200"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/view_2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="top\n1\n2\n3\n4\nbottom" />
</ScrollView>

このattributeを追加することで制約を忠実に守り、文字が溢れるのを防ぐことができます。
layout_constrainedWidthで横の制約を守らせることもできます。

最後に

レイアウトデザインをするうえでかなり便利です。
溢れる可能性があるときは、使用することがおススメです。

0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?