一个 Activity 中的多个视图(例如地图和列表)
这几天我一直在摆弄android。 在将视图包含在其他视图中时,我遇到了一个问题。
当使用简单的视图时,这没有问题,但是一旦视图有一些奇特的活动与之一起工作,例如ListActivity或MapActivity(当使用谷歌地图视图时)
可以说我需要一些动态类型的列表,以及谷歌地图在同一视图中查看。 由于它们都有与之相关的特定活动,我将如何创建它?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:weightSum="1">
<include android:id="@+id/include1" layout="@layout/list" android:layout_height="wrap_content" android:layout_width="match_parent"></include>
<include android:id="@+id/include2" layout="@layout/map" android:layout_width="match_parent" android:layout_height="wrap_content"></include>
</LinearLayout>
我应该用那个充气机代替吗?
我一直在寻找这个问题的解决方案,但我还没有找到。 要么我的 google fu 丢失了,要么我以错误的方式看待这个问题。
在写这篇文章并阅读此处的相关问题时,我可能已经找到了解决方案。 由于ListActivity和MapActivity只是扩展的Activity,而不是使用android SDK提供的ListActivity和MapActivity,我应该只使用普通的Activity,并自己实现这些东西,或者如果可能的话,在我自己的活动中重用它们的活动?
谁能证实这一点吗? :)
提前致谢 :)
I have been fiddling a bit with android the last few days.
I have run into an issue when including views in other views.
When working with simple views, this is no problem, but as soon as the view has some fancy activity working with it, like ListActivity or MapActivity (when using google maps view)
Lets say that i need a list of some dynamic kind, and a google maps view in the same view.
Since they both have a specific activity associated with it, how would i go about creating that?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:weightSum="1">
<include android:id="@+id/include1" layout="@layout/list" android:layout_height="wrap_content" android:layout_width="match_parent"></include>
<include android:id="@+id/include2" layout="@layout/map" android:layout_width="match_parent" android:layout_height="wrap_content"></include>
</LinearLayout>
Should i use that Inflater thingy instead?
I have been searching for a solution to this problem, but i havent found any.
Either my google fu is missing, or i'm looking at this the wrong way.
While writing this, and reading the related questions here on SO, i may have found the solution.
Since the ListActivity and MapActivity is just extended Activities, instead of using the ListActivity and MapActivity that the android SDK provide, i should just use an ordinary Activity, and implement the stuff myself, or if possible, reuse their activities in my own activity?
Can anyone confirm this? :)
Thanks in advance :)
MapView 并不总是位于 MapActivities 内部,ListView 并不总是位于 ListActivities 内部。这些活动作为围绕单一视图类型的其他活动的起点而存在。
回答您的问题:如果您想要一个同时包含 ListView 和 MapView 的活动,您只需将它们添加到活动的根视图组中即可。然后只需实现所需的方法即可使 MapView 和 ListView 正常工作。
我不确定您所说的在单个视图中拥有多个视图类型是什么意思。通常人们谈论将视图添加到视图组,例如:
在本例中,您有一个
LinearLayout
视图组,其中包含 2 个视图:一个Button
和一个ImageView
。在 Android 中,谈论包含另一个视图的 ImageView 是没有意义的。这让我认为你问题的标题应该是:
MapViews are not always inside MapActivities and ListViews are not always inside ListActivities. Those activities exist as starting points for other Activities that revolve around a single view type.
To answer your question: If you want an activity that contains both a ListView and a MapView you can simply add them to the root view group of the activity. Then simply implement the required methods to get the MapView and ListView working properly.
I'm not sure what you mean about having multiple view types in a single view. Usually people talk about adding views to view groups for example:
In this case you have a
LinearLayout
viewgroup that contains 2 views aButton
and aImageView
. In Android it doesn't make sense to talk about aImageView
containing another view.This leads me to think that the Title of you question should really be: