2014-06-12 61 views
1

我遇到了問題。在我的應用程序中,我有一些(3-4)按鈕需要自動縮放到全屏尺寸。 (不是隻有一個按鈕,但所有的一起)。目前,我正在爲我的應用使用LinearLayout。它可以做到這一點嗎? 對不起,我的英語不好。Android:將幾個按鈕縮放到屏幕大小

問候尼爾斯

遊戲「Dont't挖掘白」的那個屏幕類似於像我想它:

+0

設定寬度「FILL_PARENT」爲每個按鈕 –

+0

我認爲3水平模式​​相對佈局與父母的左側和父母權利可以幫助你。只是其中一種方法閃現我的想法 – Umitk

+0

3垂直LinearLayout中具有相同權重的水平面向linearLayout – user3455363

回答

2

可以使用重量概念吧。你的按鈕在所有屏幕上看起來都一樣。

首先在兩個水平部分劃分屏幕,每個寬度重量爲50 50,高度與父對齊。然後在高度添加3個按鈕的每個零件與重量爲33.33如下:

<?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="horizontal" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="50" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:text="Button" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="50" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:text="Button" /> 

    </LinearLayout> 

</LinearLayout> 
+0

您可以用更乾淨的值替換您的權重,如「1」,因爲它是用於計算分佈的這些值的實際總和剩餘的 – jcxavier

+0

jcxavier我們可以使用1或100兩者來分配權重 –

0

巴頓設置寬度match_parent。然後它將按比例縮放以適合父母。

0

是的。您可以使用LinearLayout來做到這一點。你必須設置機器人:layout_weight =「」到buttons..it取決於水平和垂直方向給出的權重的按鈕的數目..

+0

請告訴我你有多少個按鈕水平和垂直..然後我會在這裏發佈LinearLayout的代碼.. –

+0

好的..我有相同的代碼作爲上述答案,你已經接受..但我正在等待我們的迴應給出你想要的確切答案......任何方式......我想我的答案值得投票:) –

0

使用該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:weightSum="2" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="Button" 
      android:layout_weight="1" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="Button" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="Button" 
      android:layout_weight="1" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="Button" 
      android:layout_weight="1" /> 
    </LinearLayout> 


</LinearLayout>