2011-06-22 22 views
1

通常我只是分開活動,但我想顯示一個屏幕,其中有一個視圖文本視圖,一個列表,然後是一個地圖視圖。我不知道該怎麼做,因爲我知道這個活動只能擴展listview或mapview。如何在同一屏幕上顯示mapview和listview?

我有這樣一個XML文件,我們把它叫做main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/relativeLayout1" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="fill_parent" android:layout_width="fill_parent"> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" android:background="#ffffff"> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:id="@+id/tvName" 
     android:textColor="@color/dark" android:text="Name"></TextView> 
    <EditText android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:id="@+id/etName"></EditText> 
    <Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:id="@+id/btnAdd" 
     android:text="Add location"></Button> 
    <ListView android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:id="@android:id/list"></ListView> 
</LinearLayout> 

和另一個XML文件,這只是一個大地圖

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <com.google.android.maps.MapView 
     android:id="@+id/mapsView" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:clickable="true" 
     android:apiKey="myapikey" /> 

主要活動使列表中的第一個xml表單膨脹,但是如何將地圖添加到此列表中?我是否嵌套擴展地圖活動類的類,以及如何添加視圖? 這是我在想什麼

public class AddLocationActivity extends ListActivity{ 


public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 



} 
public class ShowMap extends MapActivity{ 

      public ShowMap(){ 

      //maybe get the map.xml and add the view somehow? 
      } 



    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

}

+2

爲什麼不能擴展MapActivity並使用ListView作爲其成員。 – mkso

+0

http://stackoverflow.com/a/30644926/3871524 試試這個..它非常幫助我在Listview中顯示地圖。 –

回答

3

像mkso評論,你不需要從ListActivity延長使用ListView,看到this for an example(不是最好的,但我會很快找到) 。只需創建您想要的佈局,從MapActivity延伸並使用ListView,請致電findViewById()