如何在同一屏幕上显示地图视图和列表视图?
通常我只会将活动分开,但我想显示一个具有视图文本视图、列表和地图视图的屏幕。我不知道如何去做,因为我知道活动只能扩展列表视图或地图视图。
我有一个像这样的 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;
}
}
}
Usually I would just seperate out the activities but I want to show a screen that has a view text views, a list, and then a map view. I'm not sure how to go about doing that because I know the activity can only extend either listview or mapview.
I have one xml file like this we'll call it the 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>
and another xml file that is just a big map
<?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" />
The main activity inflates the first xml sheet with the list, but then how do I add the map to this? Do I nest a class that extends the map activity class and some how add the view?
This is what I was thinking
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像 mkso 评论的那样,您不需要从
ListActivity
扩展来使用ListView
,请参阅 这是一个例子(不是最好的,但我可以很快找到)。只需按照您想要的方式创建布局,从MapActivity
扩展并通过调用findViewById()
使用ListView
即可。Like mkso commented, you do not need to extend from
ListActivity
to use aListView
, see this for an example (not the best, but what I could find quickly). Just create the layout how you want it, extend fromMapActivity
and use theListView
by callingfindViewById()
.