0

私はこのatmのようなビューを持っています:

ここに画像の説明を入力

最初の 3 つの黒い行が画面の ~70% を埋めるようにします。画面サイズがどうなるかは問題ではありません。レイアウトに重みを割り当てることで実現できると思いましたが、それほど簡単ではないようです。ご覧のとおり、ボタンで実現しましたが、常に幅に合わせて伸びています。

したがって、このxmlのコードは次のとおりです。

 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    android:orientation="vertical" 
    android:id="@+id/main"
    android:weightSum="10" //mean summary 100%

    > 
<LinearLayout
    android:id="@+id/lin1"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="3"    
    android:layout_weight="2.3"   //23% for each row 
     >
    <Button
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="1" 
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Layout Normal" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
       android:layout_weight="1" 
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1" 
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad3" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lin2"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_marginTop="2dp"
    android:orientation="horizontal"
    android:weightSum="3" 
    android:layout_weight="2.3" >

    <Button
        android:id="@+id/button4"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad4" />

    <Button
        android:id="@+id/button5"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad5" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad6" />
</LinearLayout>



<LinearLayout
    android:id="@+id/lin3"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_below="@id/lin2"
    android:orientation="horizontal"
    android:weightSum="3"
    android:layout_weight="2.3"  >

    <Button
        android:id="@+id/button7"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad7" />


    <Button
        android:id="@+id/button8"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad8" />

    <Button
        android:id="@+id/button9"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/greenbtn"
        android:onClick="onClick"
        android:text="Pad9" />
</LinearLayout>

    <LinearLayout
    android:id="@+id/lin5"
    android:layout_width="fill_parent"
    android:layout_below="@id/lin3"
    android:layout_height="60dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:layout_weight="2.1"  > //21% for toggles

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClick"
        android:textOff="Loop1 Off"
        android:textOn="Loop1 ON" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClick"
        android:textOff="Loop2 Off"
        android:textOn="Loop2 ON" />

    <ToggleButton
        android:id="@+id/toggleButton3"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClick"
        android:textOff="Loop3 Off"
        android:textOn="Loop3 ON" />

    <ToggleButton
        android:id="@+id/toggleButton4"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClick"
        android:textOff="Loop4 Off"
        android:textOn="Loop4 ON" />
</LinearLayout>


<LinearLayout
    android:id="@+id/lin6"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
     android:layout_weight="1"       //10% for webview
     >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="50dp"

    />
</LinearLayout>
</LinearLayout>

ご覧のとおり、私の考えでは、各黒い行に画面の 23% (23x3=69%)、トグルの場合は 21%、webview の場合は 10% を割り当てました。

どんな助けでも大歓迎です。

4

2 に答える 2

1

@Daler、重みを使用して高さのバランスをとろうとしているため、layout_height を 0dp に設定する必要があります

また、あまりにも多くの LinearLayouts を使用しているように思えます。おそらく、メモリ、処理時間、応答性を節約するためにいくつかを削除できます。私が推測するようなもの:

<Linear Layout - vertical - match_parent >

   // 0.269 * 3 = 0.7
   <linear layout 1 - horizontal weight = 0.269 layout_height=0dp> the three views </>
   <linear layout 2 - horizontal weight = 0.269 layout_height=0dp> the three views </>
   <linear layout 3 - horizontal weight = 0.269 layout_height=0dp> the three views </>
    // the other 30%
   <linear layout 4 - horizontal weight = 0.15 layout_height=0dp> the three views </>
   <linear layout 5 - horizontal weight = 0.15 layout_height=0dp> the three views </>


</end the top linear layout>

これは、黒い領域の 3 行が 70% であると想定しています。それがあなたのイメージから私が理解したものです。

于 2013-01-09T12:27:07.380 に答える
0

あなたが置くなら

 android:layout_weight="1"   

weightSum が 10 の場合、デフォルトで 90% かかります。ボタンを任意のレイアウトにわずか 10% で表示したい場合は、weight 属性を 9 に設定する必要があります (weightsum が 10 の場合)。

WeightSum と Weight Attributes の詳細については、こちらを参照してください。それが役立つことを願っています。

于 2013-01-09T12:24:09.093 に答える