すべてのサイズのデバイスに合わせてlayout_weight
を使用してアンドロイドレイアウトを作成しようとしています。Android layout_weight comportment
layout_width
/layout_height
を変更すると、layout_weight
に影響していましたが、わかりません。
たとえば、垂直のLinearLayout
を3つの内側のLinearLayout
に分割して、上部の1つと下部の1つが画面の25%を満たし、中央の50%がオンになるようにしたいとします。それは内部のレイアウトの内容に依存すべきではありません。 (インナーLinearLayout
の内容が大きすぎる場合、それは他のレイアウトをシフトべきではありません)
これを行うために、私はfill_parent
またはwrap_content
にインナーLinearLayout
のlayout_height
属性を設定する必要がありますか?
ありがとうございます!
EDIT:layout_weightは、レイアウトが占めるサイズに反比例するようです。
3例:
重量1/1/1(私が期待どおりに動作し)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FF0000"/> //RED
<LinearLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00FF00"/> //GREEN
<LinearLayout
android:id="@+id/layout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"/> //BLUE
</LinearLayout>
結果:
重量1/2/1(なぜ、ああ、なぜ?)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FF0000"/> //RED
<LinearLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00FF00"/> //GREEN
<LinearLayout
android:id="@+id/layout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"/> //BLUE
</LinearLayout>
結果:
**重量3/2/3(私は1/2/1をどうするentended何):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#FF0000"/> //RED
<LinearLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00FF00"/> //GREEN
<LinearLayout
android:id="@+id/layout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000FF"/> //BLUE
</LinearLayout>
結果:あなたが設定する必要があり
@Egor:これを実行しようとしましたが、この場合layout_weightがどのように動作するのか分かりません。1/1/1を入力すると、3つの部分が等しくなります。しかし、私が1/2/1を置くと、私は2番目のものを見ません。そして、スクリーンはちょうど最初のレイアウトの50%と3番目のレイアウトの50%で分割されます。どのように機能するのですか? – nbarraille
@nbarraille:スペースを指定して配布する必要があるので、問題のレイアウトを投稿して、その特定のケースで何が問題かをよりよく評価できるようにする必要があります。 – dmon
@dmon:最初の投稿を例文で更新しました。 – nbarraille