我创建了一个包含两个选项卡的活动。其中一个标签是地图(googlemapsapiv2)。
不存在activty的布局(不调用setcontent()),因为根据所选选项卡,“活动”屏幕将动态填充一个片段。
现在,我需要在地图底部添加一个文本视图。
我想我可以扩展MapFragment
然后使用onCreateView
方法,以便返回包含map和textview的不同视图。我不知道该怎么做。有什么建议吗?
编辑:
actionbar.addTab(actionbar.newTab()
.setText("LIST")
.setTabListener(new TabListener<ListFragment>(
this, "list", ListFragment.class)));
Log.i("TabListener","chiamata TABLISTENER 2");
actionbar.addTab(actionbar.newTab()
.setText("MAP")
.setTabListener(new TabListener<CustomMapFragment>(
this, "map", CustomMapFragment.class)));
tablistener类:
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(Activity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
最佳答案
您需要将mappfragment(或supportmappfragment)放置在framelayout或relativelayout中。然后,可以将textview添加到与mappfragment的同级布局中。也许实现这一点的最简单方法是将mappfragment子类化并重写oncreateview方法。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
FrameLayout wrapper = new FrameLayout(...);
TextView tv = new TextView(...);
wrapper.addView(view);
wrapper.addView(tv);
return wrapper;
}