画面上のレイアウトの固定比率
-
12-12-2019 - |
質問
私のアプリには 2 つの線形レイアウトがあります。1 つは上部に、もう 1 つは下部にあります。
私はそれが欲しいです これらのレイアウトの中にあるものは何でも, 、上部のレイアウトは画面の高さの 60% を占め、下部のレイアウトは 40% を占めます。私の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:layout_weight="1.0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="vertical"
android:id="@+id/layout_top">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.4"
android:id="@+id/layout_bottom">
</LinearLayout>
</LinearLayout>
これらのレイアウトが空の場合は問題ありません。適切な比率が保たれています。
問題は、たとえば、上部のレイアウトに小さなリストビューを配置すると、その場合、レイアウトはリストビューのサイズを取得し、60/40% の比率は維持されません。
リストビューが小さい場合でも(たとえば、項目が 3 つだけ)、レイアウトでは 60% が維持されるため、リストビューの下に空きスペースを置くようにしたいと考えています。
変えようとしてきました android:layout_height
に match_parent
しかし何も変わりません。
解決
このレイアウトを使ってみてください
<?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:layout_weight="1.0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="6"
android:orientation="vertical"
android:id="@+id/layout_top"
android:background="#FF0000">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="4"
android:id="@+id/layout_bottom"
android:background="#FF00FF">
</LinearLayout>
</LinearLayout>
.
トリックは、縦長モードでwrap_contentの代わりにlayout_height="0dip"
を設定し、Landscapeモードでwrap_contentの代わりにlayout_with="0dip"
を設定することです。
他のヒント
layout_weight LayoutFotビューに追加のスペースを指定します。のような画面を測定してみてください。
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getHeight();
.
幅* 0.6と0.4のような数量の計算をすると、このようにしてレイアウトは常に60 4の比率を持ちます。
これが役に立つことを願っています
試す layout_height = 0dp
両方の子供たちの中で LinearLayouts
. 。それはあなたが持っているために起こっています wrap_content
これはおそらくオーバーライドしています layout_weight
効果。
単に親レイアウトで、android:layout_weight="1.0"
をandroid:weightSum="1.0"
に置き換えます。
これは、親レイアウトの重み合計と子レイアウトの重みを設定することによって機能します。子供たちの体重は親の重み合計に等しくなければなりません。この http://blog.stylingandroid.com/archives/312