当前位置: 首页 > 工具软件 > Tool Drawer > 使用案例 >

ToolBar Drawer Palette

宓博实
2023-12-01

让主题没有action bar

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

定义toolbar layout

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                   android:id="@+id/tool_bar"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:background="@color/colorAccent"
                                   android:orientation="vertical">

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

main layout中设置

<include layout="@layout/my_tool_bar"/>

代码中找到,设置给activity

mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);

其他设置
标题,图标

//在设置给activity的bar前写,之后没效果
        mToolbar.setTitle("Tool Bar");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setLogo(R.drawable.ic_action_name);

menu目录下

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search_bar"
        android:orderInCategory="1"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/share_bar"
          android:orderInCategory="2"
          android:title="@string/share"
          app:showAsAction="ifRoom"/>

    <item android:id="@+id/configuration"
          android:orderInCategory="3"
          android:title="@string/configuration"
          app:showAsAction="never"/>
</menu>

监听menu

mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.share_bar:
                        Toast.makeText(MainActivity.this, "share bar", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.configuration:
                        Toast.makeText(MainActivity.this, "configuration", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }

                return true;
            }
        });

设置抽屉布局

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/darker_gray">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/click_recycler"/>

        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/star"
                android:gravity="center"
                android:text="@string/interface_content"
                android:textColor="@android:color/white"/>
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

设置监听按钮组合

 private DrawerLayout mDrawer;
    private ActionBarDrawerToggle mDrawerTool;
//放在设置了之后,放前面,设置了后没效果,关联不起来
        setLeftCornerClick();
        private void setLeftCornerClick() {
        //是否给左上角图标的左边加上一个返回的图标
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //放后面,放在放图标的后面,放前面没效果
        mDrawerTool = new ActionBarDrawerToggle(this, mDrawer, mToolbar, R.string.open, R.string.close);
        mDrawerTool.syncState();
        mDrawer.addDrawerListener(mDrawerTool);

设置调色板设置tool bar的背景颜色

private void palette() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star);
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                Palette.Swatch swatch = palette.getVibrantSwatch();
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(swatch.getRgb()));
            }
        });
    }

设置setSupportActionBar(mToolbar);
new ActionBarDrawerToggle(this, mDrawer, R.string.open, R.string.close);后用下面方法关联

//action bar的项目的选择
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            //抽屉打开的?(抽屉的部位)
            if (mDrawer.isDrawerOpen(GravityCompat.START)) {
                mDrawer.closeDrawers();
            } else {
                //抽屉打开(抽屉的部位)
                mDrawer.openDrawer(GravityCompat.START);
            }
        }
        return super.onOptionsItemSelected(item);
    }
 类似资料:

相关阅读

相关文章

相关问答