0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

レイアウトで最大幅を設定する

Posted at

LinearLayoutでwidthをlayout_widthにした場合、maxWidthの値は無視されては300dpに制限されない。
親幅を指定しつつ最大幅を優先設定する方法をメモする。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:maxWidth="300dp"
        android:text="親の幅に合わせる" />
</LinearLayout>

以下のようにConstraintLayoutでラップし、layout_constraintWidth_maxを使用すれば親幅に合わせつつ最大幅を設定できる。
幅の設定はlayout_constraintHeightや最小幅(min)も設定できる。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:maxWidth="300dp"
        android:text="最大幅幅に合わせる"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_max="300dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?