2017-08-04 72 views
-1

我想製作一個基本的計算器應用程序,所以我開始爲每個數字和操作製作按鈕。爲了讓它們對齊,我使用了一個RelativeLayout,但在它下面我有五個LinearLayout,每個LinerarLayout代表一排按鈕。如何對齊兩個LinearLayouts?

我的問題是以下,我使用layout_alignBottom第一行爲了保持它在屏幕的底部,我試圖用layout_above對齊第一行上面的第二行,但似乎我可以'噸做到這一點,因爲我得到一個No resource found that matches the given name (at 'layout_above' with value '@id/firstRow').錯誤,實際上我得到這個錯誤的所有對齊屬性。

這裏是我的xml的一部分

<LinearLayout 
    android:id="@+id/secondRow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@id/firstRow"> 

    <Button 
     android:id="@+id/b1" 
     android:layout_width="85dp" 
     android:layout_height="85dp" 
     android:text="1" /> 

     <!-- more buttons --> 


</LinearLayout> 

<LinearLayout 
    android:id="@+id/firstRow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"> 


    <Button 
     android:id="@+id/bP" 
     android:layout_width="85dp" 
     android:layout_height="85dp" 
     android:layout_marginLeft="0dp" 
     android:text="." /> 

    <!-- more buttons --> 

</LinearLayout> 

我該如何調整的LinearLayouts在方式,第一行是在屏幕的底部,其它的是一個每個人的上面?

回答

0

XML是從頂部呈現爲底部,所以,如果你想添加一個引用是要添加的參考一前一後的元素,你需要調用@+id,而不是@id。在你的情況,你應該這樣寫:

android:layout_above="@+id/firstRow" 

這是因爲當時你正在從代碼的第一LinearLayout,Android不具有id爲「FIRSTROW」,因爲它是沒有登記任何元素所以你需要添加+這意味着如果稍後找到任何引用,它將被設置,如果沒有,它只是對齊沒有引用的視圖。

0

你正在以比所需的更復雜的方式來做到這一點。

只是將它們包裝成垂直方向的線性佈局。

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

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

     <Button 
      android:id="@+id/b1" 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:text="1" /> 

    <!-- more buttons --> 

    </LinearLayout> 

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

     <Button 
      android:id="@+id/b1" 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:text="3" /> 

    <!-- more buttons --> 

    </LinearLayout> 

    <!-- Your rows in order from top to bottom --> 

</LinearLayout> 
0

嘗試使用android:layout_toStartOfandroid:layout_toEndOf