Toolbar功能比默认的ActionBar更强大更丰富,如今按照先后渐入顺序记录Toolbar的配置(添加toolbar和配置menu)。
1、修改styles,增加noactionbar状态:
2、res里新建menu文件夹,同时新建文件,如menu_main.xml,增加menu和item配置:
xml version="1.0" encoding="utf-8"?>
3、修改layout内的主xml布局文件,如activity_main.xml,增加ToolBar,此处仅列出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"?>xmlns:android="http://schemas.android.com/apk/res/android" package="com.reality.realityapp"> android:name="android.permission.INTERNET" /> 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"> android:name=".ui.activity.SplashActivity" android:theme="@style/AppTheme_FullScreen"> android:name="android.intent.action.MAIN" /> android:name="android.intent.category.LAUNCHER" /> android:name=".ui.activity.MainActivity" /> android:name=".ui.activity.LoginActivity" /> android:name=".ui.activity.FirstPageActivity" /> android:name=".ui.activity.RegisterActivity" /> android:name=".ui.activity.ThemeSelectActivity" /> android:name=".ui.activity.NewsInfoActivity" android:hardwareAccelerated="true" /> android:name=".ui.activity.RankInfoActivity" /> android:name=".ui.activity.ThemeNewsListActivity" /> android:name=".ui.activity.base.BaseActivity">
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); }