Androidレイアウトでのウェイト(weight)プロパティの使用
Android開発では、レイアウトを必要な割合で等分する必要がある場合や、コントロールが必要なサイズが必要です.
は、指定したコントロールのサイズ以外のすべてのスペースです.この時私たちが使うべきandroid:layout_weightプロパティを設定します.
1、各コントロールの占有スペースの割合に基づいて重みを指定できます.たとえば、最初のviewの重みを2、2番目のviewの重みを1に設定すると、最初のviewは2/3の空間を占め、2番目のviewは1/3の空間を占めます.重みを設定する前に一般的にViewの幅または高さを0 dpに設定すると、上記の重みルールに基づいて各コントロールが占める空間の大きさが計算されます.多くの場合viewにmatch_を設定するとparentプロパティでは、上の計算ウェイトは正比例ではなく反比例で計算されます.つまり、ウェイトが大きいほど、逆に占める空間が小さくなります.
2、androidのすべてのviewの重みはデフォルトで0です.1つのviewの重みだけが0より大きい場合、このviewは他のviewが占める空間以外のすべての空間を占めます.
1つ目の場合
効果は次のとおりです.
2つ目の効果のコードは次のとおりです.
は、指定したコントロールのサイズ以外のすべてのスペースです.この時私たちが使うべきandroid:layout_weightプロパティを設定します.
1、各コントロールの占有スペースの割合に基づいて重みを指定できます.たとえば、最初のviewの重みを2、2番目のviewの重みを1に設定すると、最初のviewは2/3の空間を占め、2番目のviewは1/3の空間を占めます.重みを設定する前に一般的にViewの幅または高さを0 dpに設定すると、上記の重みルールに基づいて各コントロールが占める空間の大きさが計算されます.多くの場合viewにmatch_を設定するとparentプロパティでは、上の計算ウェイトは正比例ではなく反比例で計算されます.つまり、ウェイトが大きいほど、逆に占める空間が小さくなります.
2、androidのすべてのviewの重みはデフォルトで0です.1つのviewの重みだけが0より大きい場合、このviewは他のviewが占める空間以外のすべての空間を占めます.
1つ目の場合
<?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="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK" />
</LinearLayout>
効果は次のとおりです.
2つ目の効果のコードは次のとおりです.
<?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="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
で得られた効果は: