1

私は自分用のカー ドック アプリケーションを作成しており、アプリケーションへのショートカットを配置できる 6 つのボタンがあります。横向きモードと縦向きモードのレイアウトを設計しました。ポートレート モードでの画面サイズに問題があります。アイコンを次のように並べています。

Text up here
------------
| 1  |  2  |
------------
| 3  |  4  |
------------
| 5  |  6  |
------------

ボタンの XML は次のとおりです。

<Button
    android:id="@+id/btnLaunchSlotOne"
    style="@style/launchButton"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_weight="1"
    android:layout_marginRight="8dp"
    android:padding="10dp"
    android:text="Navigation"
    android:textColor="#ffffff" />

今私が抱えている問題は、アプリを小さな画面サイズでテストすると、ボタンのサイズが変更されないようで、下部の画面から消えてしまうことです。それらを適切にスケーリングするにはどうすればよいですか? 「dp」を使用すると、すべてが処理されると思いましたか?

プログラムで画面サイズを計算する必要がありますか?

提案/助けてくれてありがとう!

4

4 に答える 4

3

以下のレイアウトのようなものを使用して、ボタンを正しくネストします。

<LinearLayout
    android:orientation="vertical">
    <TextView>Put your Text here</TextView>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_weightSum="2">
        <LinearLayout
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weightSum="3"
            android:layout_weight="1">
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_weightSum="3"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1">
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

次に、各ボタンの高さは0dp(これにより、ウェイトが適用され、ボタンが水平方向に拡大縮小されます)と親の塗りつぶしの幅が必要になります。

秘訣は、子要素の重みにより、0dpに設定されたディメンションが親要素のweightSumのパーセンテージまで満たされることです。

たとえば、weightSum 3と重み1の子ボタンが3つある線形レイアウトがあります。高さが0dpに設定されている場合、それぞれが親の高さの1/3を高さとして使用します。widthが0dpに設定されている場合、それぞれが親の幅の1/3を自分のものとして使用します。

これは、幅または高さのパーセンテージに従ってアイテムを整理するための最良の方法です。

余談ですが、1つはウェイト2、もう1つはウェイト1の2つのボタンがある場合、1つのボタンは親の高さ/幅の33%になり、他のボタンは親の高さ/幅の66%になります。便利なちょっとしたトリックで、すべての画面で機能します。

于 2012-04-20T16:43:53.220 に答える
2

ボタンを常に全画面表示にしたい場合は、GridViewすべての画面サイズで自動的にスケーリングを処理する を使用できます。

于 2012-04-20T16:34:30.757 に答える
0

ここで例を見てください。特にダッシュボードのレイアウト

于 2012-04-20T17:50:51.150 に答える
0

以下のxmlファイルを使用できます:::

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView 
 android:text="Text 1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 /> 
 <LinearLayout 
  android:orientation="horizantal" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weight_sum = "2">
 <Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
 <LinearLayout 
  android:orientation="horizantal" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weight_sum = "2">
 <Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
 <LinearLayout 
  android:orientation="horizantal" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weight_sum = "2">
 <Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
</LinearLayout>


</LinearLayout>
于 2012-04-20T16:42:15.387 に答える