2012-04-25 45 views
-2

在我的android應用程序中,我有兩個按鈕,位於一行上方,並顯示一張地圖。我希望每個按鈕佔據一半的行。誰能告訴我該怎麼做?我如何設置按鈕來填充每一行?

目前我的代碼是:

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btnProgram" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Program" /> 

    <Button 
     android:id="@+id/btnAfisareStatii" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Afisaza toate statiile" 
     android:layout_toRightOf="@+id/btnProgram"/> 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/btnAfisareStatii" /> 

    <WebView 
     android:id="@+id/webview2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/webview" /> 

    <com.google.android.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:apiKey="0ws8opHdf-pIbZUa916nju81kSSlPRK4Wbg_0vg" 
     android:clickable="true" 
     android:enabled="true" 
     android:layout_above="@id/btnProgram"> 
    </com.google.android.maps.MapView> 

</RelativeLayout> 

回答

2

使用線性佈局和layout_weight:

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/button_container" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/btnProgram" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:text="Program" /> 

     <Button 
      android:id="@+id/btnAfisareStatii" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/btnProgram" 
      android:text="Afisaza toate statiile" /> 
    </LinearLayout> 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/btnAfisareStatii" /> 

    <WebView 
     android:id="@+id/webview2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/webview" /> 

    <com.google.android.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/button_container" 
     android:apiKey="0ws8opHdf-pIbZUa916nju81kSSlPRK4Wbg_0vg" 
     android:clickable="true" 
     android:enabled="true" > 
    </com.google.android.maps.MapView> 

</RelativeLayout> 

但你爲什麼把網頁視圖下方的按鈕,其自身排列在屏幕的底部?

+0

我有一個錯誤在該行按鍵

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" android:layout_weight="1"/> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" android:layout_weight="1"/> </LinearLayout> 

weight屬性:機器人:layout_width = 「match_parent」。錯誤是:錯誤:錯誤:字符串類型不允許(在'layout_width'值'match_parent')。 – Andreea 2012-04-25 12:22:04

+0

然後放入fill_parent。這兩個值意味着相同,但fill_parent從API版本8起重新命名爲match_parent。 – Renard 2012-04-25 12:23:13

+0

仍然不適合我。我收到一個錯誤,如下所示:應用程序意外停止 – Andreea 2012-04-25 12:27:40

2

將您的按鈕放在LinearLayout中,並將layout_weight分配給它們。然後把這個LinearLayout放到你的RelativeLayout的底部。

<LinearLayout layout_width="fill_parent" layout_height="fill_parent"> 
    <Button 
     android:id="@+id/btnProgram" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Program" /> 

    <Button 
     android:id="@+id/btnAfisareStatii" 
     android:layout_width="wrap_fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Afisaza toate statiile" /> 
</LinearLayout> 
+0

我做到了,但現在只顯示地圖,按鈕不再出現 – Andreea 2012-04-25 12:29:11

1

放置一個LinearLayout內兩個按鈕,並設置android:orientation="horizontal"

的東西,如下。請確保您有更新的

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <WebView 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/btnAfisareStatii" 
     android:layout_weight="1"/> 

    <WebView 
     android:id="@+id/webview2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/webview" 
     android:layout_weight="1"/> 

    <com.google.android.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:apiKey="0ws8opHdf-pIbZUa916nju81kSSlPRK4Wbg_0vg" 
     android:clickable="true" 
     android:enabled="true" 
     android:layout_weight="1"> 
    </com.google.android.maps.MapView> 

     <LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
     android:layout_weight="1"> 
      <Button 
     android:id="@+id/btnProgram" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Program" 
     android:layout_weight="1"/> 

    <Button 
     android:id="@+id/btnAfisareStatii" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Afisaza toate statiile" 
     android:layout_toRightOf="@+id/btnProgram" android:layout_weight="1"/> 

    </LinearLayout> 
</LinearLayout> 
+0

我這樣做了,但現在只顯示地圖,按鈕不再出現 – Andreea 2012-04-25 12:29:31

+0

檢查更新後的答案,看看是否有幫助。 – 2012-04-25 12:47:50

+0

我記下你的更新,按鈕顯示爲我想要的,但不好看。在頁面的上半部分沒有顯示任何東西,中間顯示了一些地圖和按鈕,並在底部沒有顯示任何東西 – Andreea 2012-04-25 12:59:22