与 Activity 分开打开的片段未显示操作栏

Fragment opened separately from Activity doesnt show up actionbar

我一直在使用 NavigationDrawer activity。我一直在调用 activity 中的片段。调用成功但操作栏未显示。

代码:Gallery_Frag - activity 创建用于扩展我想要显示的片段。

public class Gallery_Frag extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_bar_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (savedInstanceState == null){
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new GalleryFragment()).commit(); }
    }

代码:GalleryFragment

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
       /* final TextView textView = root.findViewById(R.id.text_gallery);
        galleryViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });*/
        return root;
    }}

代码:清单

<activity android:name=".Gallery_Frag"
    android:label="My Bookings"
    android:theme="@style/AppTheme.NoActionBar"/>

代码:app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

代码:content_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</RelativeLayout>

首先,您必须使用包含 Toolbar

setContentViewlayout 设置为 Activity
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set it first
    setContentView(R.layout.your_activity);


    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (savedInstanceState == null){
        getSupportFragmentManager().beginTransaction()
                .add(R.id.content, new GalleryFragment()).commit(); }
}

这个content_main.xml里面可能有一个容器;

如果是,将该 ID 传递给 add(R.id.container_id, new GalleryFragment())...

否则将<include layout="@layout/content_main" />改为

<include layout="@layout/content_main" android:id="@+id/fragment_container"/>

并使用

add(R.id.fragment_container, new GalleryFragment())...

知道了

在您的 content_main.xml 文件中,将 id 添加到父级相对布局,如下所示,

<RelativeLayout
    android:id="@+id/fragment_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

并替换,

getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new GalleryFragment()).commit();

getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new GalleryFragment()).commit();

这有望奏效。