动作栏不可见?

时间:2018-11-16 11:01:19

标签: android android-studio android-toolbar

在我的android应用程序中,我有一个抽屉,但是为了获得抽屉,我必须手动单击并拖动该抽屉。如何将符号添加到工具栏? (通过点击我要处理我的抽屉),我还如何使该工具栏在所有其他布局中可见?

这是我的activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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"
    android:id="@+id/drawer"
    tools:context=".drawer">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?android:attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/header_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        app:itemTextColor="@color/grey"
        app:itemIconTint="@color/grey"
        app:menu="@menu/menudrawer"
        android:layout_gravity="start">

    </android.support.design.widget.NavigationView>

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

这是我的抽屉.java

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class drawer extends AppCompatActivity {
private DrawerLayout mDrawer;
private ActionBarDrawerToggle mToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.Open,R.string.Close);
        mDrawer.addDrawerListener(mToggle);
        mToggle.syncState();
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (getSupportActionBar()!=null){
            setSupportActionBar(toolbar);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item))
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

3 个答案:

答案 0 :(得分:0)

尝试使用此版本的操作栏抽屉开关

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);

答案 1 :(得分:0)

关于工具栏的问题,您需要对此进行更改:

if (getSupportActionBar()!=null){
    setSupportActionBar(toolbar);
}

仅此:

setSupportActionBar(toolbar);

,它应该可以工作。 关于在其他屏幕上传播此行为-您要么需要:

  1. 在各处添加工具栏和抽屉

  1. 将此作为基本活动中的基本布局 并继承基本活动,在子级上添加适当的布局

答案 2 :(得分:0)

使用以下代码根据您的需要帮助您

 ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.str_drawer_o, R.string.str_drawer_clos)
if (getSupportActionBar()!=null)
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_nav_icon, this.getTheme());
    actionBarDrawerToggle.setHomeAsUpIndicator(drawable);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawer(GravityCompat.START);
            } else {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        }
    });

    actionBarDrawerToggle.syncState();