2013-06-02 224 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

感谢的,这项工作的了。 :) –