2012-11-01 104 views
0

我很困惑。我想要顯示地圖並在地圖下方顯示5個按鈕。我使用RelativeLayout,但程序只顯示產品按鈕。爲什麼?我很困惑我使用哪種佈局(線性,相對,框架或絕對)!請幫幫我。以及如何更正此代碼?我應該使用哪種佈局?

location.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/frame" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<com.google.android.maps.MapView 
    android:id="@+id/mapView" 
    android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/background" 
    android:layout_gravity="bottom" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button_home" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/home_icon" 
     android:text="@string/button_home" 
     android:textColor="@color/text_home" /> 

    <Button 
     android:id="@+id/button_product" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/product_icon" 
     android:onClick="Product" 
     android:text="@string/button_product" 
     android:textColor="@color/text_product" /> 

    </LinearLayout> 
    </LinearLayout> 
+0

我建議你從我的經驗中使用線性佈局。 –

+0

它修復了您必須在地圖下方添加5個按鈕。或其動態? –

回答

1

回答您的特定問題:您應該說產品按鈕位於主頁按鈕的右側,而不是說主頁按鈕位於產品按鈕的左側。當RelativeLayout膨脹時,佈局將以線性方式分析,因此如果視圖A相對於視圖B定位,則視圖B必須先出現。

<Button 
    android:id="@+id/button_home" 
    style="?android:attr/borderlessButtonStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/home_icon" 
    android:text="@string/button_home" 
    android:textColor="@color/text_home"/> 

<Button 
    android:id="@+id/button_product" 
    style="?android:attr/borderlessButtonStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/button_home" 
    android:drawableTop="@drawable/product_icon" 
    android:onClick="Product" 
    android:text="@string/button_product" 
    android:textColor="@color/text_product" /> 

將此添加到產品按鈕並從主頁按鈕中刪除layout_toLeftOf。

android:layout_toRightOf="@id/button_home" 

您可以使用重力和定位來定位home鍵,然後有其他的四個按鈕下面的話,每一個定位到其前一個的權利。

祝你好運

+0

非常感謝。該程序顯示按鈕爲true。 –

0
  • 的LinearLayout:當我們需要安排 窗口小部件的LinearLayout使用/視圖在水平或垂直方式。 排列方向可以設置爲水平或垂直,默認情況下爲 。

  • TableLayout:如果佈局的控件/視圖需要的行和列的形式排列 ,我們用這個佈局對象。 這與html表類似。單元格可以跨越列。 TableLayout不顯示其邊框。我們可以通過設置列的各個屬性來製作 縮小和拉伸, 「TableRow」是另一個幫助器小部件,它應該與TableLayout結合使用 。

  • RelativeLayout:這裏每個widgets/view的位置是相對/相互依賴的 。例如,當需要佈局 時,使其在編輯文本框的左側有一個文本視圖,在EditText的下面有一個按鈕 。視圖之間的關係在 一次迭代中被關注,因此如果視圖B的位置依賴於視圖A的位置,則視圖A必須在佈局中排在第一位。

  • 的FrameLayout:這是一個非常簡單地佈局,其用於保持屏坯件的一部分 ,在運行時顯示項目的項目或組。在framelayout中添加的所有 元素將被添加到屏幕的左上角。

  • AbsoluteLayout:當有需要時,以指定確切的x和y座標的視圖 位置,然後AbsoluteLayout需要被使用。這種佈局是難以維護的 。

+0

謝謝。我想,我爲我的程序使用LinearLayout或RelativeLayout。這是真的嗎?但是當我使用LinearLayout時,程序只顯示地圖,而不顯示按鈕。爲什麼?或者當我使用RelativeLayout時,程序只顯示產品按鈕不顯示主頁按鈕。爲什麼?我編輯第一篇文章,並使用LinearLayout編寫程序 –

+0

@PariyaDaghoghi:在您的

+0

你想要這樣嗎?b1 b2 b3 b4 b5。?在彼此旁邊和上方查看地圖? –

0

RelativeLayout默認將這兩個按鈕放在一起,所以你可以看到後者。

併線

android:layout_toLeftOf="@+id/button_product" 

是錯誤的。 @+id創建一個id,在這種情況下使用@id

我會建議LinearLayout的情況。放置這些按鈕,並用一些保證金進行調整。