【问题标题】:Edit toolbar according to the fragment displaying根据片段显示编辑工具栏
【发布时间】:2018-03-27 00:15:02
【问题描述】:

在我的主要活动中,我有一个导航抽屉,通过单击它,我可以加载不同的片段,例如 Home 、 Cart 、 Settings 等。最初在我的主要活动中,我得到了带有微调器和按钮的工具栏导航抽屉也是如此。但是对于其他片段,我需要根据需要自定义工具栏。

以下是我的 activity_main.xml 的实现方式:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="@bool/fitsSystemWindows">

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/status_bar_kitkat_height"
            android:background="?colorPrimary" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/status_bar_lollipop_height"
            android:background="?colorPrimaryDark" />

    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/status_bar_margin_top">

        <FrameLayout
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?actionBarSize" />

            <!--include different toolbars into the activity dont know this is correct implementation... If I missed any of them It gives me a null pointer exception in MainActivity..-->


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

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


    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="@bool/fitsSystemWindows"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/navigation_drawer_menu"
        app:theme="@style/NavigationViewTheme" />

</android.support.v4.widget.DrawerLayout>

然后我实现了一个根据片段加载(在主活动内部)自定义工具栏的方法:

public void customizeToolBar(int position) {
        switch (position) {
            case 0:
                toolbar = (Toolbar) findViewById(R.id.toolbar_home);
                setSupportActionBar(toolbar);
                subCategories_spinner = (Spinner) findViewById(R.id.spinner_subCategories);
                actionBar = getSupportActionBar();
                actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setDisplayShowTitleEnabled(false);
                break;
            case 1:
                toolbar = (Toolbar) findViewById(R.id.toolbar_cart);
                setSupportActionBar(toolbar);
                actionBar = getSupportActionBar();
                actionBar.setTitle("Check Out");
                actionBar.setDisplayHomeAsUpEnabled(true);
                break;
                ....

然后我在 NavigationItemSelectedListener 中调用该方法,如下所示:

private void setupNavigationDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.item_navigation_drawer_home:
                                menuItem.setChecked(true);
                                customizeToolBar(0);
                                setFragment(0);
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_cart:
                                menuItem.setChecked(true);
                                customizeToolBar(1);
                                setFragment(1);
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                                ...

但这不起作用。每次显示相同的工具栏。我是安卓新手。任何使这项工作的建议将不胜感激。谢谢你。

【问题讨论】:

    标签: android android-fragments android-toolbar


    【解决方案1】:

    这个问题有很多解决方案。

    如果您包含不同的工具栏,您可以添加 set android:visibility=""

    例如:

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="#2196F3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">
        </android.support.v7.widget.Toolbar>
    

    并在更改片段时设置工具栏可见性:

    toolbar.setVisibility(View.VISIBLE);

    或者您可以在片段中编辑工具栏:

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    

    从 Main Activity 获取工具栏并进行编辑。

    【讨论】:

    • 好回答黄阮。我用你的第一个解决方案做到了。我不知道 toolbar.setVisibility(View.VISIBLE); 。我只是 Android 开发的初学者。我之前尝试过你的第二个解决方案。但是,如果我没有在 main_activity 中包含工具栏,它总是会给我空指针异常。一旦包含,我的显示中就会得到多个工具栏。无论如何,非常感谢您的解决方案。 :)
    • 没问题。使用第二种解决方案,它将获得您包含在 Main Activity 中的工具栏,您可以像这样更改视图:stackoverflow.com/questions/3334048/… 抱歉,我忘记了。
    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多