【转】Android新组件Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等
【转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46619637 CSDN 废墟的树】
今天来学习总结一下,Android 后添加的一些新的组件和UI效果,Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等。
Material Dialog
你还在为使用 Material Dialog 去引用第三方的library包么?现在告诉你一个好消息,其实Android 在V7包里面已经实现了 Material 风格的对话框,并且兼容到底版本了。你只需要在你的代码中使用V7中的Dialog即可实现以上图片效果了。代码如下:
private void showDialog1() {
android.support.v7.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("让我们一起飞,我带着你,你带着钱,来一场说走就走的旅行")
.setNegativeButton("取消", null)
.setPositiveButton("确定", null)
.setTitle("Material Design Dialog")
.show();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
是不是很赞,和之前的Dialog使用无任何差别,妈妈再也不用担心我使用Material Dialog对话框了。
SwipeRefreshLayout
原来谷歌已经实现了 Material Design 风格的下拉刷新组件,这个新的组件SwipeRefreshLayout是ViewGroup在V4包下面,你只需按照如下使用:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--可滑动的组件,比如ScrollView,ListView,GridView,等-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--添加自己的内容-->
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
SwipeRefreshLayout组件下包裹一个可滑动的组件即可实现下拉刷新效果。然后在Java代码中使用如下:
swipeRefreshLayout = findView(R.id.swipe_container);
//设置下拉刷新监听事件
swipeRefreshLayout.setOnRefreshListener(this);
//设置进度条的颜色
swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
//设置圆形进度条大小
swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//设置进度条背景颜色
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.DKGRAY);
//设置下拉多少距离之后开始刷新数据
swipeRefreshLayout.setDistanceToTriggerSync(50);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
其中包括以下常用方法:
- setColorSchemeColors() 设置进度条颜色,可设置多个值,进度条颜色在这多个颜色值之间变化
- setSize() 设置下拉出现的圆形进度条的大小,有两个值:SwipeRefreshLayout.DEFAULT 和 SwipeRefreshLayout.LARGE
- setProgressBackgroundColorSchemeColor()设置圆形进度条背景颜色。
- setDistanceToTriggerSync() 设置手势操作下拉多少距离之后开始刷新数据
总结:当然 SwipeRefreshLayout 组件有很多不足之处,比如没有上拉刷新这个功能,不过网上已经有人实现了这一效果,想要的可以自己网上搜一把吧。
LinearLayoutCompat
最近在V7包中突然发现 LinearLayoutCompat 组件,处于好奇,百度了一把这个组件的作用:用于给LinerLayout 中的子元素item之间设置间隔线的,效果图如下:
你还在为给每个LinerLayout 的item元素添加分割线烦恼么?告诉你,不用烦恼啦!android 给你现成的组件,你只需简单配置即可。代码参考如下:
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:orientation="vertical"
app:divider="@drawable/line"
app:dividerPadding="25dp"
app:showDividers="middle|beginning|end">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:text="CSDN 废墟的树"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:text="CSDN 废墟的树"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:text="CSDN 废墟的树"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:text="CSDN 废墟的树"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:text="CSDN 废墟的树"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="10dp"
android:text="CSDN 废墟的树"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.LinearLayoutCompat>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本,所以你必须的引用 V7包下面的LinearLayoutCompat。 LinearLayoutCompat除了拥有LinerLayout原本的属性之外,主要有如下几种属性来实现 间隔线效果。
- app:divider=”@drawable/line” 给分隔线设置颜色,这里你需要在drawable在定义shape资源,否则将没有效果。看下面
- app:dividerPadding=”25dp” 给分隔线设置距离左右边距的距离。
- app:showDividers=”middle|beginning|end” 分隔线显示的位置,有四种参数值:middle 每个item之间,beginning最顶端显示分隔线,end 最底端显示分隔线,none不显示间隔线。
注意 这三个属性需要使用 xmlns:app=”http://schemas.android.com/apk/res-auto” 命名空间
app:divider=”@drawable/line” 的资源代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/material_blue_grey_800" />
<!--需要设置高度,否则不显示-->
<size android:height="1px" />
</shape>
- 1
- 2
- 3
- 4
- 5
- 6
总结:以后你还需要自己画分割线么?看完LinearLayoutCompat组件是不是很高兴啊!!哈哈哈
ListPopupWindow
PopupWindow的简单实用,无需更多的去自定义,获取去确定PopupWindow的位置等,你只需要使用ListPopupWindow就能满足你简单的 PopupWindow 弹出框的使用了。直接上代码:
public void showListPopup(View view) {
String items[] = {"item1", "item2", "item3", "item4", "item5"};
final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
//设置ListView类型的适配器
listPopupWindow.setAdapter(new ArrayAdapter<String>(SwipeRefreshActivity.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) {
Toast.makeText(SwipeRefreshActivity.this, "the position is" + position, Toast.LENGTH_SHORT).show();
// listPopupWindow.dismiss();
}
});
//设置ListPopupWindow的锚点,也就是弹出框的位置是相对当前参数View的位置来显示,
listPopupWindow.setAnchorView(view);
//ListPopupWindow 距锚点的距离,也就是相对锚点View的位置
listPopupWindow.setHorizontalOffset(100);
listPopupWindow.setVerticalOffset(100);
//设置对话框的宽高
listPopupWindow.setWidth(300);
listPopupWindow.setHeight(600);
listPopupWindow.setModal(false);
listPopupWindow.show();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
根据以上代码,你可以做如下事情:
- listPopupWindow.setAnchorView(view); 设置弹出框显示的位置
- listPopupWindow.setHorizontalOffset(100);距离锚点View水平距离
- listPopupWindow.setVerticalOffset(100); 距离锚点View的垂直距离
- listPopupWindow.setWidth(300);设置弹出框的大小
不用解释了吧!代码都有注释。望君自己研究然后去手写代码,这样学习更快。
PopupMenu
菜单弹出框,效果如下:
代码如下:
public void showPopupMenu(View view) {
//参数View 是设置当前菜单显示的相对于View组件位置,具体位置系统会处理
PopupMenu popupMenu = new PopupMenu(this, view);
//加载menu布局
popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu());
//设置menu中的item点击事件
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
//设置popupWindow消失的点击事件
popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
}
});
popupMenu.show();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
总结:PopupMenu 相对ListPopupWindow可定制化比较少。
Spinner
流行风格的下拉类别组件。你只需要在XML布局中使用 新的style主题即可实现如上效果
<Spinner
android:id="@+id/spinner"
style="@android:style/Widget.Holo.Light.Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Spinner>
- 1
- 2
- 3
- 4
- 5
后续如有发现好用的,美观的UI组件会继续添加。今天就暂且到这里吧,退朝!
【转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46619637 CSDN 废墟的树】
【转】Android新组件Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等的更多相关文章
- Android新组件RecyclerView介绍,其效率更好
今天我们首先来说为什么要介绍这个新组件RecyclerView,因为前几天我发布了一个常用面试题ListView的复用及如何优化的文章,介绍给一些开发者,但是我看到有关的反馈说:现在都不再用listv ...
- android新组件RecyclerView使用介绍和进阶使用,替用Gallery
简介: RecyclerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,但是直接把viewholder的实现封装起来,用 ...
- 【转】android新组件RecyclerView使用介绍和进阶使用,替用Gallery
简介: RecyclerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,但是直接把viewholder的实现封装起来,用 ...
- Android新组件CardView
Android L以后,新增了一个CardView组件,Google官方应用中有不少地方是使用卡片来展示信息,背后应该就是这个CardView. 使用CardView要引入单独的support包:co ...
- 【Android】进入Material Design时代
由于本文引用了大量官方文档.图片资源,以及开源社区的Lib和相关图片资源,因此在转载的时候,务必注明来源,如果使用资源请注明资源的出处,尊重版权,尊重别人的劳动成果,谢谢! Material Desi ...
- Android四大组件之一“广播”
前言 Android四大组件重要性已经不言而喻了,今天谈谈的是Android中的广播机制.在我们上学的时候,每个班级的教室里都会装有一个喇叭,这些喇叭都是接入到学校的广播室的,一旦有什么重要的通知,就 ...
- Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...
- Android 四大组件之“ BroadcastReceiver ”
前言 Android四大组件重要性已经不言而喻了,今天谈谈的是Android中的广播机制.在我们上学的时候,每个班级的教室里都会装有一个喇叭,这些喇叭都是接入到学校的广播室的,一旦有什么重要的通知,就 ...
- Android业务组件化之子模块SubModule的拆分以及它们之间的路由Router实现
前言: 前面分析了APP的现状以及业务组件化的一些探讨(Android业务组件化之现状分析与探讨),以及通信的桥梁Scheme的使用(Android业务组件化之URL Scheme使用),今天重点来聊 ...
随机推荐
- highcharts报表插件之expoting参数的使用
exporting 参数配置 本文转载自:http://blog.csdn.net/myjlvzlp/article/details/8531275 说明:导出及打印选项 打印导出功能的配置项. 1. ...
- MIB-II
. 1.3.6.1.2.1
- [转] - JAR文件包及jar命令详解 ( MANIFEST.MF的用法 )
常常在网上看到有人询问:如何把 java 程序编译成 .exe 文件.通常回答只有两种,一种是制作一个可执行的 JAR 文件包,然后就可以像. chm 文档一样双击运行了:而另一种是使用 JET 来进 ...
- nova.conf部分参数解析
#----------networking options---------------# #nova的dhcpbridge配置的文件位置 --dhcpbridge_flagfile=/etc/nov ...
- CSS3系列:魔法系列
一.三角形 #wrap div{ margin: 0 auto; } .triangle_three { height:0px; width:0px; border-bottom:50px solid ...
- ibatis动态sql配置启动时提示:The content of elements must consist of well-formed character data...
ibatis动态sql配置启动时提示:The content of elements must consist of well-formed character data... 2012-07-18 ...
- 安装wamp,访问主页提示PHP configuration loaded file……
安装wamp,访问主页提示PHP configuration loaded file…… *** ERROR *** The PHP configuration loaded file is: - s ...
- DS实验题 Old_Driver UnionFindSet结构
题目 思路 很典型的并查集问题,朋友A和B要合并到一个统一的集合中,也就是Union(A, B)操作,在Union操作中需要先找到A所属的集合的代表元和B所属集合的代表元,也就是使用Find(A)和F ...
- PHP 错误与异常 笔记与总结(11 )register_shutdown_function() 函数的使用
通过 register_shutdown_function 方法,可以让我们设置一个当执行关闭时可以被调用的另一个函数. 也就是说,当我们的脚本执行完成或者意外死掉导致 php 执行即将关闭时,我们的 ...
- MySQL DATE_FORMAT() 函数
定义和用法 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的输出 ...