如何用样式替换setSupportActionBar?

时间:2019-07-09 08:32:11

标签: android android-toolbar androidx

在我的Android应用中,我无法使用AppCompatActivity。 我没有该项目这部分的源代码。 所以我用

androidx.fragment.app.FragmentActivity

应用主题:

<style name="MainAppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

我的带有工具栏的布局:

<androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolBar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/tool_bar_height"/>
        <!-- Framelayout to display Fragments -->
        <FrameLayout
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
        </FrameLayout>
    </LinearLayout>    

</androidx.drawerlayout.widget.DrawerLayout>

这是我的代码段:

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
public class MainNavigationDrawerFragmentActivity extends androidx.fragment.app.FragmentActivity

mToolbar = (Toolbar) findViewById(R.id.toolBar);
toolbaTitleTextView = (TextView) findViewById(R.id.toolbaTitleTextView);
actionBarDrawerToggle = new ActionBarDrawerToggle(
    this,
    mDrawerLayout,
    mToolbar, 
    R.string.application_name,
    R.string.application_name) {
        public void onDrawerClosed(View view) {
            toolbaTitleTextView.setText(mTitle);
            invalidateOptionsMenu();
        }

结果显示工具栏,但菜单不显示。 要显示菜单,我需要使用

setSupportActionBar(toolbar);

但是我不能使用此方法,因为我不能使用AppCompatActivity

是否可以用其他方法或设置/样式替换setSupportActionBar(toolbar);以便在工具栏上显示菜单?

0 个答案:

没有答案