Android Toolbar使用技巧之添加toolbar和配置menu

Toolbar功能比默认的ActionBar更强大更丰富,如今按照先后渐入顺序记录Toolbar的配置(添加toolbar和配置menu)。

1、修改styles,增加noactionbar状态:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

2、res里新建menu文件夹,同时新建文件,如menu_main.xml,增加menu和item配置:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"

    tools:context="com.reality.realityapp.ui.activity.NewsInfoActivity">
    <item
        android:id="@+id/id_menu_report"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:icon="@drawable/report"
        android:title="举报"
        />
</menu>

3、修改layout内的主xml布局文件,如activity_main.xml,增加ToolBar,此处仅列出toolbar的布局配置:

<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/id_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/deepred"
    app:titleTextColor="@color/white"
    app:navigationIcon="@drawable/back">

4、修改资源文件,即manifests文件夹下的AndroidManifest.xml文件,增加AppTheme.NoActionBar风格配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.reality.realityapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".ResApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/releaselogo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/releaselogo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ui.activity.SplashActivity"
            android:theme="@style/AppTheme_FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ui.activity.MainActivity" />
        <activity android:name=".ui.activity.LoginActivity" />
        <activity android:name=".ui.activity.FirstPageActivity" />
        <activity android:name=".ui.activity.RegisterActivity" />
        <activity android:name=".ui.activity.ThemeSelectActivity" />
        <activity
            android:name=".ui.activity.NewsInfoActivity"
            android:hardwareAccelerated="true" />
        <activity android:name=".ui.activity.RankInfoActivity" />
        <activity android:name=".ui.activity.ThemeNewsListActivity" />
        <activity android:name=".ui.activity.base.BaseActivity"></activity>
    </application>

</manifest>

5、在activity中获取toolbar并给toolbar配置menu,覆写onCreateOptionMenu(添加Menu)和onOptionsItemSelected(菜单项被点击的事件监-)代码如下:

Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
setSupportActionBar(toolbar);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_news,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.id_menu_report:
            AlertDialog.Builder builder=new AlertDialog.Builder(NewsInfoActivity.this);
            builder.setIcon(android.R.drawable.ic_dialog_info);
            builder.setTitle("请选择你举报的原因");
            final String []itemsId=new String[]{"内容虚假","盗版侵权","内容低俗","危险言论","封建迷信"};
            final boolean []checkedItems=new boolean[]{false,false,false,false,false};//这里的true是默认第几个人已经被选中
            builder.setMultiChoiceItems(itemsId, checkedItems,new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean ischeck) {
                    checkedItems[which]=ischeck;
                }
            });
            //设置一个确定按钮
            builder.setPositiveButton("确定", null);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setCancelable(false);
            final AlertDialog dialog = builder.create();
            dialog.show();
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.d("checkedItems", "checkedItems: "+checkedItems);
                    String text="";
                    boolean hasSelected=false;
                    for(int i=0;i<itemsId.length;i++)
                    {
                        text+=checkedItems[i]?itemsId[i]+",":"";
                        if (checkedItems[i]){
                            hasSelected = checkedItems[i];
                            break;
                        }
                    }
                    if (hasSelected) {
                        T.showToast("举报成功!感谢您的反馈!");
                        dialog.dismiss();
                    }
                    else {
                        T.showToast("还未选择举报原因!");
                        return;
                    }
                }
            });
            break;
    }
    return super.onOptionsItemSelected(item);
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页