横方向レイアウトで「android:layout_weight」を使用する際、「android:layout_width」を"0dp"にしないとどうなるの?

レイアウトlayout_weight

2パターン

 状態:未解決  閲覧数:3,689  投稿日:2014-05-11  更新日:2014-05-22
・大きく下記2パターンに分かれる


パターン1


「android:layout_weight」×「android:layout_width="wrap_content"」
・「android:layout_weight」を設定している要素に「android:layout_width="wrap_content"」を指定
・正常表示される
・しかし、「layout_weightでの幅計算」と「wrap_contentの幅計算」が両方行われてしまうため無駄
・レイアウト表示パフォーマンスを向上させるためには、「android:layout_weight」使用する際、「android:layout_width="0dp"」を指定する方が良い


パターン2


「android:layout_weight」×「android:layout_width="match_parent"」
・「android:layout_weight」を設定している要素に「android:layout_width="match_parent"」を指定
・正常表示されない(ことがある)
・法則性は不明

パターン2 … 「android:layout_weight」を使用(1-2-3)しているのに、「android:layout_width」を"0dp"にしていない

 閲覧数:575 投稿日:2014-05-12 更新日:2014-05-22


概要


・Button1が画面の2/3、Button2が1/3を占拠し、Button3に至っては画面から消えている

原因
・横方向で「android:layout_weight」を使用しているのに、「android:layout_width」を"0dp"にしていない


構成


・1つのLinearLayoutの中で配置

LinearLayout
 │ 
 ├LinearLayout
 │ └Button
 │ └Button
 │ └Button 
 │ 
 └LinearLayout

・最後のLinearLayoutでは、何も表示していない


コード


▼/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="1"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="2"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="3"
           android:text="3"/>
   </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1">
       
   </LinearLayout>
</LinearLayout>



詳細


何らかの法則性があるように見えるのは、たまたまそうなっているだけ、なことが後に判明

余白は負の値になり得る
・Buttonを3つ配置し、全てのlayout_widthをmatch_parentにした場合、余白は0でなく、Buttonの横幅(match_parentなので親ビューの幅)2つ分マイナスになる

負の値に対するweightの動き
・Button横幅を1とすると、余白は-2となり、各Buttonのweightは、下記のようになる
-2 * (設定したlayout_weight / 各Buttonのlayout_weight合計値)

Button1が画面の2/3になる理由
・横幅の1/3分マイナス
-2 * (1/6) = -0.333
1 + (-0.333) = 0.667

Button2が画面の1/3になる理由
・横幅の2/3分マイナス
-2 * (2/6) = -0.666
1 + (-0.666) = 0.334

Button3が表示されない理由
-2 * (3/6) = -1
1 + (-1) = 0


パターン2 … 「android:layout_weight」を使用(1-4-3)しているのに、「android:layout_width」を"0dp"にしていない

 閲覧数:661 投稿日:2014-05-12 更新日:2014-05-22


概要


・Button1が画面の3/4、Button3が1/4を占拠し、Button2に至っては画面から消えている

原因
・横方向で「android:layout_weight」を使用しているのに、「android:layout_width」を"0dp"にしていない


構成


・1つのLinearLayoutの中で配置

LinearLayout
 │ 
 ├LinearLayout
 │ └Button
 │ └Button
 │ └Button 
 │ 
 └LinearLayout

・最後のLinearLayoutでは、何も表示していない


コード


▼/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="1"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="4"
           android:text="4"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="3"
           android:text="3"/>
   </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1">
       
   </LinearLayout>
</LinearLayout>



詳細


何らかの法則性があるように見えるのは、たまたまそうなっているだけ、なことが後に判明

余白は負の値になり得る
・Buttonを3つ配置し、全てのlayout_widthをmatch_parentにした場合、余白は0でなく、Buttonの横幅(match_parentなので親ビューの幅)2つ分マイナスになる

負の値に対するweightの動き
・Button横幅を1とすると、余白は-2となり、各Buttonのweightは、下記のようになる
-2 * (設定したlayout_weight / 各Buttonのlayout_weight合計値)

Button1が画面の3/4になる理由
・横幅の1/4分マイナス
-2 * (1/8) = -0.25
1 + (-0.25) = 0.75

Button3が画面の1/4になる理由
・横幅の3/4分マイナス
-2 * (3/8) = -0.75
1 + (-0.75) = 0.25

Button2が表示されない理由
-2 * (4/8) = -1
1 + (-1) = 0


パターン2 … 「android:layout_weight」を使用(1-4-3-5)しているのに、「android:layout_width」を"0dp"にしていない

 閲覧数:581 投稿日:2014-05-12 更新日:2014-05-22


概要


・Button5が画面途中で千切れている

原因
・横方向で「android:layout_weight」を使用しているのに、「android:layout_width」を"0dp"にしていない


構成


・1つのLinearLayoutの中で配置

LinearLayout
 │ 
 ├LinearLayout
 │ └Button
 │ └Button
 │ └Button 
 │ 
 └LinearLayout

・最後のLinearLayoutでは、何も表示していない


コード


▼/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="1"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="4"
           android:text="4"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="3"
           android:text="3"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="3"
           android:text="5"/>        
   </LinearLayout>


</LinearLayout>



詳細


・法則性云々以前の問題として、画面表示が明らかにおかしい
・一応上記計算式に当てはめようと努力してみるも散々な結果に終わる…




類似度ページランキング
順位 ページタイトル抜粋
1 横方向レイアウトで「android:layout_weight」を使用する際、「android:layout_width」を"0dp"にしないとどうなるの? 59
2 エミュレータ利用時、「adb shell」ルートパスが、Windows全体ではどの位置にあるか知りたい 33
3 「android compile with 」プルダウン内容が表示されないため、プロジェクト作成完了ボタンをクリックできない 30
4 Android Studio の Designプレビュー で、ウィジェットをドラッグ配置できない 29
5 Android Studio 3.1.2 で、パレットの「Widgets」内にSpinnerが表示されないため、選択出来ない 29
6 Android Studio の Designプレビュー で、ウィジェットを中央に配置 28
7 「Android Studio 3.0.1」で、デザインプレビュー画面が表示されない。「waiting build for finish」が終わらない 27
8 「Android Studio 3.1.2」のデザインタブで、RadioGroupの中にRadioButtonを配置出来ない 27
9 データベースを利用するためには、 SQLiteOpenHelperクラスが必要なの? 27
10 「Android Studio 3.1.2」でプロジェクト削除するためには、物理的にキーボードの「Delete」キーを押す必要がある 26
11 「Android Studio 3.0.1」でプロジェクトを選択するためには? 最初に「現在開いているプロジェクト」を閉じる必要がある 25
12 プラグイン "org.eclipse.ui.workbench" からのコードの起動で問題が発生しました 25
13 「Android Studio 3.1.2」で「Propertiesペイン」が表示されない 24
14 Android用と思われるLogCatビューが、LogCat(使用すべきではありません)と表示されている理由 24
15 「Android Studio 3.0.1」でデザインプレビューで表示されるテキストと、エミュレーター(もしくは実機デバッグ)で表示されるテキスト内容が異なる 24
16 「SQLite」利用しているのに、データベースが見つからない 24
17 Unexpected error while launching logcat 24
18 Dimension "◇◇" in attribute "layout_width" is missing unit! 23
19 run build する際、選択画面が表示されない 23
20 values-ja/strings.xmlに記載した内容が反映されない 23
2024/9/21 15:36 更新
週間人気ページランキング / 9-14 → 9-20
順位 ページタイトル抜粋 アクセス数
1 インポートされた ★★ は見つかりません | エラー 57
2 ○○は解決できないか、フィールドではありません | エラー(エラー) 26
3 いくつかのプロジェクトは、ワークスペース・ディレクトリーにすでに存在するため、インポートできません | エラー 22
3 Androidエミュレータで、PCキーボード入力を有効にしたい | エミュレータ(環境構築) 22
4 public 型 ★★ はそれ独自のファイル内に定義されなければなりません | エラー 21
5 この行に複数マーカーがあります | エラー(エラー) 20
6 「Android Studio 3.0.1」で、デザインプレビュー画面が表示されない。「waiting build for finish」が終わらない | Android Studio(IDE) 13
7 プラグイン "org.eclipse.ui.workbench" からのコードの起動で問題が発生しました | エラー(エラー) 9
8 Eclipse で「プロジェクト名」「パッケージ名」を変更する方法 | Eclipse(IDE) 7
8 Android Studio で、ソースコードが改行できない | Android Studio(IDE) 7
9 Eclipseが起動しない … ユーザ操作は待機中です | Eclipse(IDE) 6
9 内部エラーの表示中に内部エラーが発生しました | エラー 6
10 既存プロジェクトがインポート出来ない | プロジェクト(環境構築) 5
10 インポートされた java.io は見つかりません | エラー(エラー) 5
11 Android FAQ 4
11 ファイルのパスをコピーする方法と結果は、右クリックする場所で異なる | Android Studio(IDE) 4
11 ★★ を型に解決できません | エラー(エラー) 4
11 java.lang.NullPointerException | エラー(エラー) 4
11 キーバインドの競合が発生しました。 通常のアクセラレーター操作を妨げる可能性があります。 | エラー(エラー) 4
12 エラーが発生しました。ログ・ファイル ★★.log.を参照してください | エラー(エラー) 3
2024/9/21 1:01 更新