2012-11-16 137 views
0

我想开发一个使用线性布局的计算器应用程序,但是我不熟悉android开发,所以我不知道是谁写的... 我该如何做到这一点例如,在一行中有3个按钮(它们占据了整个水平空间),然后是包含3个按钮的第二行?在Android开发中的线性布局

例子:

[ 7 8 9 ] 
[ 4 5 6 ] 
[ 1 2 3 ] 

回答

0

不要使用LinearLayout中。改为在RelativeLayout中使用RelativeLayouts。

您也可以使用TableLayout,但我发现RelativeLayout可以更好地适应不同的屏幕几何形状。

+0

我很抱歉但你能告诉我的例子使用相对布局? 如何写下来? 并且线性布局存在问题(仅为了解) – Tharwat7

0

如果你想坚持LinearLayout,你可能会想要这样的东西。正如其他人所指出的,TableLayout可能更强大。我发现LinearLayouts非常简单。鉴于你的应用程序的简单性,我很难相信它会强调任何Android设备,除非在屏幕上做动画和事情。另外,我相信你需要一个计算器的其他组件的空间,这应该是非常简单的放入这个布局..你可以添加另一个层次的嵌套或改变你的权重,以适应更小的行顶部显示公式/答案。

<?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" > 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="0dp" android:layout_weight="1" 
     android:orientation="horizontal"> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="7" 
     /> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="8" 
     /> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="9" 
     /> 
    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="0dp" android:layout_weight="1" 
     android:orientation="horizontal"> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="4" 
     /> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="5" 
     /> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="6" 
     /> 
    </LinearLayout> 

    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="0dp" android:layout_weight="1" 
     android:orientation="horizontal"> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="1" 
     /> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="2" 
     /> 
     <Button android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="3" 
     /> 
    </LinearLayout> 

</LinearLayout> 
+0

仅供参考,@ user1413755相同的layout_weight在线性布局内的元素之间平均分配空间。 – caiocpricci2

+0

感谢您的澄清。当我编写所​​有代码时,我感到非常惊讶,将它复制到eclipse中,并且它工作正常。第一次!但是,重量将根据内部元素的总重量分配。您可以在父LinearLayout中指定总数,但只有在您尚未放置某些动态内容(AFAIK)时才需要。 –

+0

谢谢Matt Wolfe – Tharwat7

1

我建议使用TableLayout因为LinearLayout你可能会遇到一个警告“坏性能嵌套的权重”。你需要使用很多android:weightSum来填充所有空格。 尝试下面的一个。

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="3" 
    > 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="3"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="7" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="8" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="9" 
      /> 
    </TableRow> 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="3"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="4" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="5" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="6" 
      /> 
    </TableRow> 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="3"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="3" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="2" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="1" 
      /> 
    </TableRow> 

</TableLayout> 
+0

@userlsAMonkey,您是否指定第一个权重等于3,因为有3行?还是有另一个原因? – Tharwat7

+0

你的意思是tableLayout的重量?是的,我指定了3分贝。在你的图中你有3行。 –

+0

谢谢你现在一切都很清楚:) – Tharwat7