gpt4 book ai didi

android - fragment 未显示在 Activity 顶部

转载 作者:行者123 更新时间:2023-11-29 17:47:22 24 4
gpt4 key购买 nike

我想在我点击一个项目进入我的操作栏时显示一个 fragment ,我在代码中这样做:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.create_boundary:
Fragment boundary = new BoundaryFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.map_activity, boundary);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
break;
}
}

和 fragment :

public class BoundaryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create the view of the fragment associate with the maps activity
View view = inflater.inflate(R.layout.boundary_maps, container, false);
return view;
}

fragment 的布局:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</GridLayout>

activity_map.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_activity"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

onCreate of mapsActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
[...]
}

我不明白为什么没有显示。有人能告诉我哪里出了问题吗?

提前致谢。

最佳答案

FragmentTransaction.replace(int,Fragment,String) 的官方文档所述:

Parameters
- containerViewId Identifier of the container whose fragment(s) are to be replaced.
- fragment The new fragment to place in the container.
- tag Optional tag name for the fragment, to later retrieve the fragment with FragmentManager.findFragmentByTag(String).

这意味着您在交易中错误地使用了 id。此外,您可能希望在 XML 布局中定义的那个之上添加 BoundaryFragment,因此您应该使用 FragmentTrasaction.add(int, Fragment)相反。

无论如何要解决您的问题,您应该将您的 id 移动到 RelativeLayout,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

关于android - fragment 未显示在 Activity 顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25592749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com