2013-06-02 205 views
3

我對Android開發很新穎。我目前正在開發一款應用,我需要在屏幕上佈局4個按鈕。按鈕1 + 2應該在第一行,在第二行應該是3 + 4。我希望每個按鈕的高度和寬度相同。而且,因爲有多種屏幕尺寸,我希望按鈕寬度爲屏幕寬度的40%。 這是可能的只是通過佈局,或者我需要計算代碼中的一切嗎?Android中的佈局4按鈕

注意:我想將應用程序部署到運行Android 2.2或更高版本的設備。

下面是一個示例圖形。 enter image description here

編輯:對於所有誰是尋找方形的東西..這是解決方案:http://android-layouts.com/category/tags/square

回答

3

試試這個

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.4" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
     android:text="Button" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.4" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
     android:text="Button" /> 

    </LinearLayout> 



</LinearLayout> 

enter image description here

所以解決這個問題,你必須使用android:layout_weight="40/100"="0.4"
之前它必須設置android:layout_width 0 .becuase沒有它android:layout_weight="40/100"="0.4"不工作。
更多細節aboute``安卓layout_weight`去
What does android:layout_weight mean?

UPDATE 1

做這個任務按鈕的高度嘗試下面的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0px" 
     android:layout_weight="0.6" 
     android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0px" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.4" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0px" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.6" 
     android:text="Button" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0px" 
     android:layout_weight="0.4" 
     android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0px" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.4" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="0px" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.6" 
     android:text="Button" /> 

    </LinearLayout> 



</LinearLayout> 
+0

@LucèBrùlè您是否測試我的代碼 –

+0

嗨,謝謝!這工作。如果我想讓按鈕的高度爲寬度,那麼我是否必須在代碼中執行此操作? –

+0

@LucèBrùlè嗨,好的。 –

0

我將開始把前兩個按鈕水平的LinearLayout裏面,第二個2上的另一個水平的LinearLayout 。然後添加填充按鈕以分離它們。就40%而言,你可以使用體重XML屬性。

3

在本answer指出的Romain Guy

不能使用百分比來定義的RelativeLayout內的一個視圖的尺寸。最好的方法是使用LinearLayout和權重或自定義佈局。

所以你在找什麼是LinearLayoutandroid:layout_weight屬性。

下面的代碼將創建一個從你的屏幕截圖的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="1" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="3" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="4" /> 
    </LinearLayout> 

</LinearLayout> 

而結果:

enter image description here

+0

感謝的,這項工作的了。 :) –