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>