android - fragment 未显示在 Activity 顶部

标签 android android-fragments

我想在我点击一个项目进入我的操作栏时显示一个 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/

相关文章:

android - 方向更改时 fragment View 为空

java - 在 fragment 上实现向上导航

android - 要安装什么 SDK,真的吗?

java - 向 Android 版 Google map 添加 5900 多个标记。如何高效做事

java - 改造 -- 预期为 BEGIN_ARRAY,但实际为 STRING

android - RecyclerView 在设备方向更改时保存位置

java - 将字符串数组从容器 Activity 传递到 fragment 时,getArguments() 返回 null

java - 如何在 Android 视觉 CameraSource 中添加放大/缩小手势

Android自定义对话框线性布局大小与对话框背景图像相同

android - 无法移动到嵌套子导航组件中的父 fragment ?