New Activity探索(二):BasicActivity
2016-02-23 19:42
405 查看
现在来看看BasicActivity有什么不同
OnCreate方法
public class BasicActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null) .show(); } }); }
上一篇中讲到,Toolbar是官方推荐的ActionBar的升级版组件,这边就开始用起来了。
此外,这边还有个组件,FloatingActionButton,简称FAB,悬浮的动作按钮,在谷歌的Material Design中,有着重要的地位,但是国内的设计挺少见到的。
还有一个组件映入视野,Snackbar,大多数情况下,消息弹出我们会习惯性的选择Toast,如果带有需要确认的动作,那就弹出一个Dialog。这里官方给出了一个新的选择,Snackbar在下方弹出,布局中配合CoordinatorLayout使用效果更佳(例子中就是这么使用的)。
这时候就有人开始问了,这些新组件都是SDK里面内置的么,那岂不是低版本完全没法用。
非也,看看我们的build.gradle,com.android.support:design被加入了dependencies中,也就是Android Support Design库,这个库提供了许多的新组件以支持Material Design。
菜单项
下面还有两个@Override的方法,看看@Override public 4000 boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_basic, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }
菜单右上角的按钮的添加和交互就是通过这两个方法来实现的,res文件夹下会有一个menu文件夹,存放菜单的布局,在
onCreateOptionsMenu中初始化,在onOptionsItemSelected设置交互。这些按钮会被添加到ToolBar上,虽然如此,并不需要写任何跟toolbar相关的语句。
layout布局
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="woody.android.newactivity.BasicActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_basic" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
大家在用ToolBar的时候是不是在根布局中直接就把ToolBar给扔进去了,我也是这么干的,不过看到官方的代码瞬间感觉复杂了好多,一个个看过来。
ToolBar外面包了一层AppBarLayout,看看官方的介绍
AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures.
This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it’s functionality will not work.
也就是说,为了实现更多的滑动特性,才有了它的存在,并且,外层还得用
CoordinatorLayout,所以才有了这样的布局。
至于
CoordinatorLayout配合
AppBarLayout能搞出什么效果,各位自行探索吧。
又一个styles.xml
上回讲到dimens.xml多了一个w820p的版本,这次在styles中多了v21的版本
显然,这也是为了多版本的差异化而存在的文件,v21即代表API21,5.0以上的系统会优先限应用v21下的style,这边存放的就是Toolbar在5.0以上的变化样式,提供了一些5.0以上特有的效果。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories