如何设置在Android上DrawerLayout切换按钮? [英] How to set up a DrawerLayout toggle button in Android?

查看:346
本文介绍了如何设置在Android上DrawerLayout切换按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有作为导航选项卡DrawerLayout。我把一切都成立,但现在我需要一个小按钮,将打开并关闭它(ActionBarToggle,或者类似的东西)。我如何可以设置它尽可能简单? (IM相对初学者,所以我没有处理这很好)

它只是需要在左上角的按钮(像一个YouTube应用了),用来切换抽屉打开和关闭。

编辑:我将我的code的抽屉:

 公共类MainActivity扩展ActionBarActivity {
 私人DrawerLayout mDrawerLayout;
    私人的ListView mDrawerList;
    / *** /
    公共NavDrawerListAdapter navDrawerListAdapter;
   片段片段;
        @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
            categoryItems =新的ArrayList< CategoryItem>();
            categoryItems.populate();
           navDrawerListAdapter =新NavDrawerListAdapter(这一点,categoryItems);
    mDrawerList.addHeaderView(listheader,空,假);
        //设置列表的点击监听器
        mDrawerList.setOnItemClickListener(新DrawerItemClickListener()); mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList =(ListView控件)findViewById(R.id.left_drawer);   }
}

在其他方面,有一个简单的适配器(NavDrawerListAdapter),和一个简单的JavaBean CategoryItem。还有一个小功能填充(),它只是增加了一些元素到列表中。所有这些功能完美。所有我现在需要的是在左上角有一个切换按钮来打开和关闭抽屉式导航栏。


解决方案

 公共类MainActivity扩展ActionBarActivity {
     私人ActionBarDrawerToggle切换;     @覆盖
     保护无效的onCreate(捆绑savedInstanceState){
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.activity_main);
         工具条工具栏=(栏)findViewById(R.id.toolbar_widget);
         setSupportActionBar(工具栏);
         getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
         DrawerLayout drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
         拨动=新ActionBarDrawerToggle(这一点,drawerLayout,R.string.drawer_open,R.string.drawer_close);
        toggle.setDrawerIndicatorEnabled(真);
        drawerLayout.setDrawerListener(切换);
     }     @覆盖
     公共布尔onOptionsItemSelected(菜单项项){
          返回切换= NULL&放大器;!&安培; toggle.onOptionsItemSelected(项目)|| super.onOptionsItemSelected(项目);
     }     @覆盖
     保护无效onPostCreate(捆绑savedInstanceState){
          super.onPostCreate(savedInstanceState);
          如果(切换!= NULL)
              toggle.syncState();
     }     @覆盖
     公共无效onConfigurationChanged(配置NEWCONFIG){
          super.onConfigurationChanged(NEWCONFIG);
          如果(切换!= NULL)
              toggle.onConfigurationChanged(NEWCONFIG);
     }
}

编辑:
不要忘记添加引用来支持的build.gradle库:

 编译com.android.support:support-v4:22.0.0
编译com.android.support:appcompat-v7:22.0.0

和导入所有的:

 进口android.support.v4.widget.DrawerLayout;
进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBarDrawerToggle;
进口android.support.v7.widget.Toolbar;
进口android.view.MenuItem;
进口android.content.res.Configuration;

和检查您的 styles.xml ,父主题应该是:

 <样式名称=AppTheme父=Theme.AppCompat.Light.NoActionBar>

I have a DrawerLayout used as a navigation tab. I have everything set up, but now I need a small button that would open it and close it (ActionBarToggle, or something like that). How can I set it up as simply as possible? (Im a relative beginner, so I'm not handling this very well)

it just needs to be a button in the top left corner (like the one the youtube app has) that toggles the drawer open and closed.

Edit: I'm adding my code for the drawer:

     public class MainActivity extends ActionBarActivity {


 private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    /***/
    public NavDrawerListAdapter navDrawerListAdapter;
   Fragment fragment;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
            categoryItems = new ArrayList<CategoryItem>();
            categoryItems.populate();
           navDrawerListAdapter = new NavDrawerListAdapter(this, categoryItems);
    mDrawerList.addHeaderView(listheader, null, false);
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

   }
}

Among other things, there is a simple Adapter (NavDrawerListAdapter), and a simple JavaBean CategoryItem. There is also a small function populate() which simply adds a few elements to the list. All of those function perfectly. All I need now is a toggle button in the top left corner to open and close the navigation drawer.

解决方案

public class MainActivity extends ActionBarActivity{
     private ActionBarDrawerToggle toggle;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_widget);
         setSupportActionBar(toolbar);
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
         toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
        toggle.setDrawerIndicatorEnabled(true);
        drawerLayout.setDrawerListener(toggle);
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
          return toggle != null && toggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
     }

     @Override
     protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          if (toggle != null)
              toggle.syncState();
     }

     @Override
     public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          if (toggle != null)
              toggle.onConfigurationChanged(newConfig);
     }
}

Edited: Don't forget to add references to support library in build.gradle:

compile 'com.android.support:support-v4:22.0.0'
compile 'com.android.support:appcompat-v7:22.0.0' 

And import all of:

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.content.res.Configuration;

And check your styles.xml, parent theme should be:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

这篇关于如何设置在Android上DrawerLayout切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆