<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/box_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="item name"
android:textSize="26sp"
/>
<TextView
android:id="@+id/box_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="item name"
android:textSize="26sp"
/>
<TextView
android:id="@+id/box_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="item name"
android:textSize="26sp"
/>
</LinearLayout>
上記のandroidのレイアウトファイルで
ルートの要素linearlayout内の
子要素、textviewの高さが均等になるように、
layout_heightの属性の値を変更していただきたいのですが・・・
layout_weight あるいは、weight
という名前の属性を指定するのでしたでしょうか?
はい、`layout_height` 属性を均等にするには、
`layout_weight` を使います。
`layout_weight` 属性を使うと、
親の `LinearLayout` の高さ(または幅)を
子要素間で分割することができます。
その場合、`layout_height` を `"0dp"` に設定し、
`layout_weight` を各子要素に指定することで、
高さを均等にすることができます。
以下は修正後のコード例です:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/box_01"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="16dp"
android:text="item name"
android:textSize="26sp" />
<TextView
android:id="@+id/box_02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="16dp"
android:text="item name"
android:textSize="26sp" />
<TextView
android:id="@+id/box_03"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="16dp"
android:text="item name"
android:textSize="26sp" />
</LinearLayout>
```
### 説明
1. **`layout_height="0dp"`**
子要素の高さは `0dp` に設定します。
これにより、`layout_weight` が高さを制御します。
2. **`layout_weight="1"`**
各 `TextView` に同じ `1` を設定することで、
親の高さを均等に分割します。
3. **`layout_margin`**
マージンはそのまま適用されますが、
均等な高さはその中で保持されます。
この設定により、
すべての `TextView` の高さが均等になります。
コメント
コメントを投稿