【android】3、Toolbar
来源:互联网 发布:三级分销佣金软件 编辑:程序博客网 时间:2024/06/08 03:12
Toolbar包含ActionBar,是一个view。通常在activity的顶端,用于标题栏等。
在使用Toolbar时,要将activity中原有的ActionBar隐藏掉,否则原有的actionBar和Toolbar会同时出现在顶端。
隐藏ActionBar:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
使用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/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <Button android:id="@+id/nav_button" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/ic_home"/> <!--城市名--> <TextView android:id="@+id/title_city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#fff" android:textSize="20sp"/> </android.support.v7.widget.Toolbar>在该布局文件中,设置了一个Button,用于显示菜单,其实可以使用toolbar中默认的返回键,而不必手动添加一个Button。添加了一个TextView,用于动态显示标题的信息。
Toolbar中右侧最多设置三个action:
在menu/toolbar.xml中列出action:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/settings" android:icon="@drawable/ic_settings" android:title="Settings" app:showAsAction="never"/></menu>
由于在项目中只用到了一个action,因此只有一个<item>
app:showAsAction="never"表示永远不显示在toolbar中,ifRoom,表示有空间就显示。“always”表示永远显示在toolbar中
如果要在toolbar中显示action,要通过
public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.toolbar, menu); return true; }设置action的布局,否则不会显示。
而action对应的操作则需要函数:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.settings:{ AlertDialog.Builder build = new AlertDialog.Builder(this); build.setTitle("自动更新?"); build.setCancelable(true); build.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); //设置内容区 final String [] items = {"自动更新","取消自动更新"}; build.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit(); editor.putString("autoUpdate", "Update"); editor.apply(); Toast.makeText(getApplicationContext(), "自动更新", Toast.LENGTH_LONG).show(); dialog.dismiss(); break; case 1: SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit(); edit.putString("autoUpdate", "UnUpdate"); edit.apply(); Toast.makeText(getApplicationContext(), "取消自动更新", Toast.LENGTH_LONG).show(); dialog.dismiss(); break; } } }); AlertDialog dialog = build.create(); dialog.show(); break; } } return true; }这里设置了当点击settings时打开一个dialog,进行相关的设置。
阅读全文
0 0
- 【android】3、Toolbar
- Android Toolbar
- android ToolBar
- android toolbar
- Android Toolbar
- Android ToolBar
- Android Toolbar
- android toolbar
- Android ToolBar
- Android Toolbar
- ANDROID Toolbar
- Android Toolbar
- 【ToolBar】Android ToolBar之完全解析
- android 控件 隐藏toolbar-(CoordinatorLayout Toolbar AppbarLayout)
- android 控件 toolbar BaseActivity集成toolbar
- android:ToolBar详解
- android:ToolBar详解
- android:ToolBar详解
- 大三夏季学期实训-千峰-css-float属性小记
- 0和5 51Nod-1433
- 前端基础知识
- shell脚本中if判断
- HDU 3613 Manacher算法
- 【android】3、Toolbar
- WSAPoll引发的一场血案(2)
- SQL Server 游标语句 声明/打开/循环实例
- ZooKeeper_15_服务器角色/数据同步
- 前端重点题目汇总
- 图书谷(www.tushugu.com)
- [3]442. Find All Duplicates in an Array/[3]448. Find All Numbers Disappeared in an Array(Java)
- nyoj 76超级台阶
- Spring Boot Configuration Annotion Processor not found in classpath