Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击菜单图标时导航抽屉未打开_Java_Android_Navigation Drawer_Android Toolbar - Fatal编程技术网

Java 单击菜单图标时导航抽屉未打开

Java 单击菜单图标时导航抽屉未打开,java,android,navigation-drawer,android-toolbar,Java,Android,Navigation Drawer,Android Toolbar,单击导航图标时导航抽屉未打开(即左上方屏幕上的3条水平线)。棒棒糖是有效的,但问题是Kitkat和果冻豆是无效的 Styles.xml <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="windowActionBar">fal

单击导航图标时导航抽屉未打开(即左上方屏幕上的3条水平线)。棒棒糖是有效的,但问题是Kitkat和果冻豆是无效的

Styles.xml

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

}

你的代码看起来乱七八糟,看看我的,它工作得很好

public class HomeActivity extends ActionBarActivity implements
        DrawerCloseListener {
    private Toolbar toolbar;
    private DrawerLayout drawer;
    private ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        toolbar = (Toolbar) findViewById(R.id.home_toolbar);
        setSupportActionBar(toolbar);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.app_name, R.string.app_name);
        drawerToggle.setHomeAsUpIndicator(R.drawable.icon_nav);
        drawer.setDrawerListener(drawerToggle);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        toolbar.setTitle("");
        toolbar.setNavigationIcon(R.drawable.icon_nav);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
            drawer.closeDrawers();
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onDrawerClose() {
        // TODO Auto-generated method stub
        if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
            drawer.closeDrawers();
        }
    }
}

我今天面临这个问题

我想可能是您的抽屉布局在根视图中,您设置了宽度和高度=
“匹配父项”

Lolipop中,工具栏是弹出的,因此可以单击。但在KitKat
或更低版本中,它将单击抽屉布局,而不是工具栏(工具栏位于抽屉布局后面)

我的解决方案是在抽屉布局中设置
MarginTop=“?attr/actionBarSize”


希望它能帮助你认为这已经很久了:)

发布你的
活动的布局\u home
@SilentKnight请检查我已经添加了布局文件,试图将你的非抽屉视图放入FrameLayout中。将所有非抽屉放在FrameLayout中不起作用@DemonickHi shanmugapriyan,你解决了这个问题吗?。如果是,请确认如何进行?事实上,我也面临同样的问题。
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_theme_red_color"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:title="@string/app_name">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


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

    <include layout="@layout/toolbar"></include>
</FrameLayout>

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:background="#FFF"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:background="@drawable/drawer_profile_bg">

        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView_round"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="15dp"
            android:src="@drawable/disp"
            app:border_color="@color/gray_border"
            app:border_width="2dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="ex : John Mathew"
            android:textColor="@color/white"
            android:textStyle="bold" />
    </RelativeLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:choiceMode="singleChoice" />

    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />

</LinearLayout>
public class Home extends ActionBarActivity {
Toolbar toolbar;
final String[] data = {"Locate People", "Account Setting"};
DrawerLayout drawer;
ActionBarDrawerToggle mDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    initialize();
    setToolbar(0);

}

private void initialize() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            invalidateOptionsMenu();
        }


        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu();
        }
    }; // Drawer Toggle Object Made
    drawer.setStatusBarBackgroundColor(getResources().getColor(R.color.app_theme_red_color));
    drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
    mDrawerToggle.syncState();               // Finally we set the drawer toggle sync State
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}
public void setToolbar(int position) {
    if (position == 0) {
        toolbar.setTitle("Establishemnt");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    } else if (position == 1) {
        toolbar.setTitle("Settings");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    }
}
public class HomeActivity extends ActionBarActivity implements
        DrawerCloseListener {
    private Toolbar toolbar;
    private DrawerLayout drawer;
    private ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        toolbar = (Toolbar) findViewById(R.id.home_toolbar);
        setSupportActionBar(toolbar);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.app_name, R.string.app_name);
        drawerToggle.setHomeAsUpIndicator(R.drawable.icon_nav);
        drawer.setDrawerListener(drawerToggle);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        toolbar.setTitle("");
        toolbar.setNavigationIcon(R.drawable.icon_nav);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
            drawer.closeDrawers();
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onDrawerClose() {
        // TODO Auto-generated method stub
        if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
            drawer.closeDrawers();
        }
    }
}