Material Design控件-toolbar
来源:互联网 发布:淘宝上那能买到正品mlb 编辑:程序博客网 时间:2024/05/21 04:42
1.ToolBar
使用原因:ActionBar 固定在上面,扩展性不好,Google使用Toolbar来代替Actionbar
使用方法:
一。去除ActionBar,我们之前带有ActionBar是因为使用了带ActionBar的主题,所以我们得换换主题,
在AndroidManifest.xml中 找到android:theme,
ctrl +鼠标左键点击@style/AppTheme,
我们将@style/AppTheme依赖的的样式改为Theme.AppCompat.Light.NoActionBar
AndroidManifest.xml:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"><android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/colorPrimary" android:background="?attr/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="20sp" android:text="@string/app_name"/> </android.support.v7.widget.Toolbar></RelativeLayout>这里加入Textview是为了让标题居中显示,否则标题会显示在左边
步骤二:加入代码
private void initView() { toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); //设置使用toolbar setTitle(null); //设置原标题为空,这是个显示在左边的标题 }
这样就完成了。
我们看上面的动图右边还有一个menu按键,怎么加入呢?
步骤一:在menu文件夹下加入toolbar.xml文件,内容如下:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/download" android:icon="@drawable/listen" android:title="下载" app:showAsAction="always"/></menu>
步骤二:加入代码
//加载该menu文件
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar,menu); return true; }
//设置点击menu按键时的行为,这里我们是提示You click download
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.download: Toast.makeText(this,"You click download",Toast.LENGTH_SHORT).show(); break; default: break; } return true; }
0 0
- Material Design控件-toolbar
- Material Design控件之Toolbar
- Material Design 之 ToolBar
- 【Material Design】Toolbar使用
- Material Design之Toolbar
- Material Design之Toolbar
- Android Material Design Toolbar
- Material Design之ToolBar
- Material Design之Toolbar
- Material Design之ToolBar
- Android Material Design控件学习NavigationView和Toolbar使用
- Material Design以及ToolBar实战
- Material Design系列--Toolbar详细
- Material Design学习之Toolbar
- Material Design之使用Toolbar
- Material Design——Toolbar
- Android Material Design 之 Toolbar
- Material Design 系列之 Toolbar
- 网络编程基础(1) : 网路模型简介+同步阻塞实例
- jdk1.7 tomcat7 centos 安装
- Elasticsearch-2.3.x 升级之后注意的点
- jsp动态包含和静态包含的区别
- css2
- Material Design控件-toolbar
- React Native 热更新 注意点1
- 防止控件短时间被多次点击
- 微信小程序入门五: wxml文件引用、模版、生命周期
- 转载CSDN博客
- LNK4098: 默认库“MSVCRT”与其他库的使用冲突
- 消息队列四
- freemarker空值处理
- Redis操作