# Test5.0 **Repository Path**: 201216323/Test5.0 ## Basic Information - **Project Name**: Test5.0 - **Description**: 5.0旧特性的学习总结 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-12-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 前言: 上一篇Android 5.0的文章,小编仔细学习使用了TextInputLayout和Snackbar,不清楚的可以查看[原文链接](http://note.youdao.com/noteshare?id=832547f1f6b12fbfbeda7421e7e6017a),这一篇文章中将会全部介绍5.0中其它比较常用的控件,下面是目录: > 1. Material Dialog > 7. FloatingActionButton > 2. SwipeRefreshLayout > 3. LinearLayoutCompat > 4. ListPopupWindow > 5. PopupMenu > 6. Toolbar > 8. TabLayout(选项卡布局) > 9. AppBarLayout(程序栏布局)&& CoordinatorLayout(协作布局) > 11. CollapsingToolbarLayout(折叠工具栏布局) > 12.NestedScrollView的使用 ## Material Dialog Dialog我们在开发中经常用到,但不经常使用V7包里面的Material 风格的对话框,建议以后使用这种Dialog,效果太酷了。 ``` private void showMaterialDialog() { android.support.v7.app.AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("我爱你 爱着你就像老鼠爱大米") .setCancelable(false) .setNegativeButton("取消", null) .setPositiveButton("确定", null) .setTitle("this is a Material Design Dialog") .show(); } ``` 效果如下: ![image](http://ww4.sinaimg.cn/mw690/b0d9a523jw1fa4el7pdnfg20be07lwh3.gif) ## FloatingActionButton(悬浮的按钮) 在android.support.design.widget包下面,可以使用FloatingActionButton来做悬浮按钮,虽然这种效果我们可以使用ImageView来实现,但谷歌给我们提供了做悬浮按钮的控件我们就不用再自己创造了,查看FloatingActionButton的源代码可以发现,此控件是继承自ImageView的,所以可是使用ImageView的一切属性,但它也有属于自己及的专有属性: ``` 1. app:fabSize="normal"设置大小,有两种赋值分别是 “mini” 和 “normal”,默认是“normal” 2. app:borderWidth="10dp"设置边框的宽度 3. app:backgroundTint="@color/colorPrimary"设置FloatingActionButton的背景颜色,默认的背景颜色是Theme主题中的#ff0000 4. app:elevation="20dp"设置FloatingActionButton阴影的深度 ``` 效果如下: ![image](http://ww3.sinaimg.cn/mw690/b0d9a523jw1fa4flgfpb1g20bd07l75a.gif) 布局文件如下: ``` ``` ## SwipeRefreshLayout(下拉刷新) SwipeRefreshLayout使用这种控件做出的刷新效果现在非常见,印象比较深的就是直播APP上,它是在android.support.v4.widget包下,SwipeRefreshLayout组件下必须包裹一个可滑动的组件才可实现下拉刷新效果。 布局代码如下: ``` /*这里放多个TextView来测试*/ ``` java程序: ``` public class SwipeRefreshActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { SwipeRefreshLayout swipeContainer; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swiprefresh); swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer); //设置下拉刷新监听事件 swipeContainer.setOnRefreshListener(this); //设置进度条的颜色 swipeContainer.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN); //设置圆形进度条大小 swipeContainer.setSize(SwipeRefreshLayout.DEFAULT); //设置进度条背景颜色 // swipeContainer.setProgressBackgroundColorSchemeColor(Color.DKGRAY); //设置下拉多少距离之后开始刷新数据 // swipeContainer.setDistanceToTriggerSync(50); } @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { Snackbar.make(swipeContainer, "刷新结束", Snackbar.LENGTH_SHORT).show(); swipeContainer.setRefreshing(false); } }, 2000); } } ``` SwipeRefreshLayout的一些常用的设置都写在代码中了,在不设置进度条颜色的时候默认是黑色的,SwipeRefreshLayout只有下拉刷新的功能,但在实际开发中还需要上拉加载功能,这都可以去Github上search 。 效果如下: ![image](http://ww1.sinaimg.cn/mw690/b0d9a523jw1fa4fv5y7xpg20aq0hrac7.gif) ## LinearLayoutCompat V7包中有一个LinearLayoutCompat组件,可以给LinerLayout 中的子元素item之间设置分割线。 布局文件: ``` ``` LinearLayoutCompat控件有几个属性: ``` 1. app:divider=”@drawable/line” 给分隔线设置颜色,这里你需要在drawable在定义shape资源,否则将没有效果,不信的小伙伴can try。 1. app:dividerPadding=”25dp” 给分隔线设置填充大小。 1. app:showDividers=”middle|beginning|end” 分隔线显示的位置,有四种参数值:middle 每个item之间,beginning最顶端显示分隔线,end 最底端显示分隔线,none不显示间隔线。 ``` 效果如下; ![image](http://ww2.sinaimg.cn/mw690/b0d9a523jw1fa4go9cu6kg20aq0hr0tz.gif) ## PopupWindow PopupWindow这个控件在开发中经常用到,但V7包中还有一个PopupWindow,可以实现简单的PopupWindow的效果。 测试程序: ``` public void showListPopup(View view) { String items[] = {"item1", "item2", "item3", "item4", "item5"}; final ListPopupWindow listPopupWindow = new ListPopupWindow(this); //设置ListView类型的适配器 listPopupWindow.setAdapter(new ArrayAdapter(ListPopupWindowActivity.this, android.R.layout.simple_list_item_1, items)); //给每个item设置监听事件 listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Snackbar.make(lineContainer, "the position is" + position, Snackbar.LENGTH_SHORT) .setCallback(new Snackbar.Callback() { @Override public void onDismissed(Snackbar snackbar, int event) { super.onDismissed(snackbar, event); new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { listPopupWindow.dismiss(); } }, 500); } }) .show(); } }); //设置ListPopupWindow的锚点,也就是弹出框的位置是相对当前参数View的位置来显示, listPopupWindow.setAnchorView(view); //ListPopupWindow 水平偏移量 listPopupWindow.setHorizontalOffset(100); listPopupWindow.setVerticalOffset(100); //设置对话框的宽高 listPopupWindow.setWidth(lineContainer.getLayoutParams().width); listPopupWindow.setHeight(600); listPopupWindow.setModal(false); //设置点击外边区域不可取消 listPopupWindow.setForceIgnoreOutsideTouch(true); listPopupWindow.show(); } ``` 效果图如下: ![image](http://ww3.sinaimg.cn/mw690/b0d9a523jw1fa4gv2482ig20aq0hrdin.gif) ## PopupMenu 这是一个菜单式弹出框的控件,可以用这个控件来使用Menu中的文件,简单来使用一下。 测试程序: ``` public void showPopupMenu(View view) { //参数View 是设置当前菜单显示的相对于View组件位置,具体位置系统会处理 PopupMenu popupMenu = new PopupMenu(this, view); //加载menu布局 popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu, popupMenu.getMenu()); //设置menu中的item点击事件 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.share: Snackbar.make(line1Container, "share", Snackbar.LENGTH_SHORT).show(); break; case R.id.setting: Snackbar.make(line1Container, "setting", Snackbar.LENGTH_SHORT).show(); break; case R.id.cancle: Snackbar.make(line1Container, "cancle", Snackbar.LENGTH_SHORT).show(); break; } return true; } }); popupMenu.show(); } ``` menu代码: ``` ``` 效果图如下: ![image](http://ww3.sinaimg.cn/mw690/b0d9a523jw1fa4h1ngehxg20aq0hrmzr.gif) ## ToolBar ToolBar是谷歌在Android5.0的时候推出的,是在V7包中,是为了替换之前的ActionBar,Toolbar的出现解决了Actionbar的各种限制,Toolbar可以完全自定义和配置,下面详细说明一下具体使用。 为了能在你的Activity中使用Toolbar,你必须在工程里修改styles.xml文件里的主题风格,系统默认如下 ``` ``` manifests中使用此Style: ``` ``` 布局文件: ```