Android 无法控制工具栏的可见性

Android 无法控制工具栏的可见性,android,toolbar,Android,Toolbar,我无法通过代码行显示和删除工具栏。 它能做到这一点吗? 如果我在一个片段中,并且希望在ws-loading之后短时间显示工具栏。 我尝试了以下方法,但没有效果 工具栏 toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); if(actionBar != null) { actionBar.s

我无法通过代码行显示和删除工具栏。 它能做到这一点吗? 如果我在一个片段中,并且希望在ws-loading之后短时间显示工具栏。 我尝试了以下方法,但没有效果

工具栏

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

setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();

if(actionBar != null) {
     actionBar.setDisplayShowTitleEnabled(false);
}
main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/apk/res-auto"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:elevation="7dp">

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

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

        <FrameLayout
            android:id="@+id/fragContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="fragContainer" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/RecyclerView"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="fill_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:background="@color/primary"
            android:scrollbars="vertical">

        </android.support.v7.widget.RecyclerView>

        <LinearLayout
            android:id="@+id/footer_menu"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/ivFooter"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_gravity="bottom|center"
                android:maxWidth="690dp"
                android:src="@drawable/rodape_menu"
                android:layout_margin="5dp"/>

        </LinearLayout>

    </RelativeLayout>

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

如果将工具栏设置为actionbar会怎么样

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().hide();

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
然后,您可以获取actionbar并将其隐藏,或者将其隐藏在任何您想要的地方

编辑:如果布局中没有该视图,则将无法工作:

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


这个位于RelativeLayout根布局视图的底部。

是的,我这样做了,因为当您的命令不起作用时,getSupportActionBar().hide();试试看,效果会很好。您的代码中可能有问题。您的活动xml中真的有工具栏吗?xml中没有错误。我改变了我的问题,试图找出可能发生的事情。是的,你知道。。。您正在代码中创建不一致。您提到我的代码中存在不一致?它们是什么?我更改了xml(不同的样式)作为您的代码,甚至getSupportActionBar().hide()也不起作用。您能告诉我为什么要使用
ActionBar ActionBar=getSupportActionBar()???删除那个!您已经将工具栏设置为操作栏。
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().hide();

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />