android - 如何打开和关闭抽屉导航点击Android中的自定义操作栏图标?

标签 android

我是 Android 应用程序开发的初学者。我已经在 Android 应用程序中实现了自定义操作栏。我在自定义操作栏上添加了 2 个图像,请参阅下面的 xml 代码

    custom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/black_pattern" >

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#fff"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="8dp"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:background="@null"
            android:src="@android:drawable/ic_menu_rotate" />

    </RelativeLayout>

并在操作栏 View 中设置此 xml 文件

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ActionBar mActionBar = getActionBar();
            mActionBar.setDisplayShowHomeEnabled(false);
            mActionBar.setDisplayShowTitleEnabled(false);
            LayoutInflater mInflater = LayoutInflater.from(this);

            View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
            TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
            mTitleTextView.setText("My Own Title");

            ImageButton imageButton = (ImageButton) mCustomView
                    .findViewById(R.id.imageButton);
            imageButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                            Toast.LENGTH_LONG).show();
                }
            });

            mActionBar.setCustomView(mCustomView);
            mActionBar.setDisplayShowCustomEnabled(true);
        }
    }

但现在我想在 imageButton 单击时打开和关闭抽屉导航,该按钮包含在 custom.xml 布局中并添加到操作 baar 中。我也实现了抽屉导航,但我不明白如何在 imageButton 单击事件上显示和隐藏抽屉导航。有人可以帮助如何做到这一点

我正在尝试这种方式

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = "test";

        mPlanetTitles = new String[]{"one", "two", "three"};
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
        ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);


    }

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

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @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);
    }

最佳答案

如果您实现了抽屉导航,您可以尝试:

NavigationDrawerFragment mNavigationDrawerFragment;
DrawerLayout mDrawerLayout;

mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout);

ImageButton imageButton = (ImageButton) mCustomView
                    .findViewById(R.id.imageButton);
            imageButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                            Toast.LENGTH_LONG).show();
                    if(mDrawerLayout.isDrawerOpen(Gravity.END))
                        mDrawerLayout.closeDrawer(Gravity.START);
                    else
                        mDrawerLayout.openDrawer(Gravity.END);
                }
            });

关于android - 如何打开和关闭抽屉导航点击Android中的自定义操作栏图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777964/

相关文章:

java - 在不同用户上创建地理围栏

java - Android - 单击 ListView 项目时播放歌曲

java - (Android) API 特定样式?

android - 使用 SQLiteDatabase 记录 SQL 语句

android - 如何阻止 Zebra 打印机 RW 420 在从 Printer.getGraphics Util 发出打印图像命令时自动送入大量额外纸张?

android - 如何将最新数据 append 到 android 中的自定义基本适配器 ListView ?

android - 在 IOS 上从 Flutter 启动 Whatsapp 时出现问题

android - 如何使用 robolectric 用额外的数据测试开始的 Intent

android - Android的Bluetooth `autoConnect`参数到底有什么作用呢?

android - 嵌套权重不利于性能