2016-04-25 72 views
-3

我想在我的應用程序上放置一個地圖,然後在其上添加一個文本字段和一個按鈕。以下是我的xml文件。啓動此應用程序後,地圖顯示在屏幕上,但文本字段和按鈕不存在。即使我刪除地圖,它們也不會顯示。我在這裏錯過了什麼?當我使用0dp佈局寬度時,layout_weight在android上不起作用

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context="com.lenovo.mds.lenovopoc.MainActivity"> 

<com.amap.api.maps2d.MapView 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
<EditText 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.70" /> 
<Button 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="button" 
    android:layout_weight="0.70"/> 
</LinearLayout> 
+1

LinearLayout支持android:layout_weight –

+2

RelativeLayout不支持layout_weight;使用LinearLayout – akash93

+0

是的,你必須使用線性佈局,如果你想使用重量 – Saveen

回答

1

您不應在RelativeLayout中使用layout_weight。如果您的意思是使用LinearLayout,則MapView中的match_parent將佔用所有空格,因此您可以爲其分配layout_weight

+0

我更新了代碼使用LinearLayout,但我得到了相同的結果。我希望文字和按鈕位於地圖的頂部。我如何在LinearLayout中做到這一點? –

+0

如果要在地圖上疊加文本和按鈕,請使用RelativeLayout,不需要分配權重。 – Hiking

+0

但我想在地圖上顯示文字和按鈕覆蓋圖,並使用百分比來指定它們的寬度。我如何結合這兩種佈局來實現這一目標? –

1

RelativeLayouts不支持layout_weight爲其子。如果你想使用它,請更改爲LinearLayout。我不知道layout_weight是否接受.70格式的數字。