[Android]如何创建一个View的分割线

如何创建一个View的分割线,如上图
我们见介绍三种可以创建看起来很不错的view的分割线,如在button之间添加分割线。
这个例子是将为LinearLayout内的三个Button间添加分割线。
这三个例子可能容易实现,相信会有更好的实现办法。

1 人工添加LinearLayout的分割线
我们可以创建一个View,这个View就是分割线,只要简单在Button之间添加这个分割线就可以。
分割线的实现,如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<View android:layout_height="fill_parent" android:layout_width="1dp" android:background="#90909090" android:layout_marginBottom="5dp" android:layout_marginTop="5dp"/> |
So the whole layout, as pictured, becomes:
|
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
|
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:orientation="horizontal"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:text="Yes" android:layout_weight="1" android:id="@+id/button1" android:textColor="#00b0e4" /> <View android:layout_height="fill_parent" android:layout_width="1px" android:background="#90909090" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:id="@+id/separator1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:text="No" android:layout_weight="1" android:id="@+id/button2" android:textColor="#00b0e4" /> <View android:layout_height="fill_parent" android:layout_width="1px" android:background="#90909090" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:id="@+id/separator2" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:text="Neutral" android:layout_weight="1" android:id="@+id/button3" android:textColor="#00b0e4" /> </LinearLayout> |
2 在LinearLayout定义divider
你可以给LinearLayout设置a view divider,这很明显是个很好的解决方法,尤其是不知道LinearLayout下有多少个子Button。
这种必须是在API level 11 或者更高的API版本使用。
我们先定义这个分割线样式吧:
|
1
2
3
4
5
|
<?xml version="1.0" encoding="utf-8"?> <size android:width="1dp" /> <solid android:color="#90909090" /></shape> |
把这个分割线的样式设置给LinearLayout:
|
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
|
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:divider="@drawable/separator" android:showDividers="middle" android:orientation="horizontal"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:text="Yes" android:layout_weight="1" android:id="@+id/button1" android:textColor="#00b0e4" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:text="No" android:layout_weight="1" android:id="@+id/button2" android:textColor="#00b0e4" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:text="Neutral" android:layout_weight="1" android:id="@+id/button3" android:textColor="#00b0e4" /> </LinearLayout> |
其中最重要当然就是:
android:divider="@drawable/separator"
android:showDividers="middle"
3给容器组件设置ButtonBarStyle (默认是分割线,最容易实现方法)
As danialgoodwin mentioned in the comments, adding the buttonBarStyle to the LinearLayout will show default separators. This is also for api level 11 or higher only.
The important part here, is adding this line to the LinearLayout:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" style="?android:buttonBarStyle" android:dividerPadding="15dp" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button1" android:layout_gravity="center_vertical" /> <!-- more buttons/views --> </LinearLayout> |
You can also adjust the paddings of the view separators with the “dividerPadding” setting.
Button使用的相同的检举,所以他们之间的间距也是相同的。
当然你可为分割线设置渐变色。
原文 :http://envyandroid.com/archives/1193/view-separators
[Android]如何创建一个View的分割线的更多相关文章
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- android2.3 View视图框架源码分析之一:android是如何创建一个view的?
View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类.只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已. ...
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...
- android 自己创建一个凝视模板
android 自己创建一个凝视模板 作为一名程序猿 不仅要有一个写代码的能力,养成一个良好的编写习惯也是非常重要的. 今天给大家具体介绍一下怎样创建凝视模板,给每一个类和方法都自己手动去凝视信息也 ...
- Android下创建一个输入法
输入法是一种可以让用户输入文字的控件.Android提供了一套可扩展的输入法框架,使得应用程序可以让用户选择各种类型的输入法,比如基于触屏的键盘输入或者基于语音.当安装了特定输入法之后,用户即可在系统 ...
- 1.3、Android Studio创建一个Android Library
一个Android Library结构上与Android app模块相同.它可以包含构建一个app需要的所有东西,包括圆满,资源文件和AndroidManifest.xml.然而,并非编译成运行在设备 ...
- 1.1、Android Studio创建一个项目
Android Studio中的项目包含一个或多个模块.本节帮助你创建一个新的项目. 创建一个新的项目 如果你之前没有打开项目,Android Studio显示欢迎页面,通过点击Start a New ...
- [android] android下创建一个sqlite数据库
Sqlite数据库是开源的c语言写的数据库,android和iphone都使用的这个,首先需要创建数据库,然后创建表和字段,android提供了一个api叫SQLiteOpenHelper数据库的打开 ...
- Android Wear创建一个通知
创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了.(如果只是测试通知,则直接运行wear app就能够看到效果) 创建一个简单的wear通知分 ...
随机推荐
- JS设置Cookie,及COOKIE的限制
在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...
- 如何正确的使用uwsgi
简单的安装过程可以在这里找到,这里主要说一下如何配置uwsgi的服务,将uwsgi服务加入系统进程,你可以使用如下两种方式安装 apt-get apt-get install uwsgi 该命令会自动 ...
- IOS“多继承”
转自念茜的博客: 当单继承不够用,很难为问题域建模时,我们通常都会直接想到多继承.多继承是从多余一个直接基类派生类的能力,可以更加直接地为应用程序建模.但是Objective-C不支持多继承,由于消息 ...
- Tornado的一个开源社区
https://link.zhihu.com/?target=http%3A//www.tornadoweb.org/en/stable/ 基于Tornado的一个开源社区 GitHub - shiy ...
- android中使用intent来实现Activity带数据跳转
大家都知道startActivity()是用来切换跳转Activity的.如果想要在另个Activity中出书数据的话.只需要在源activity中使用intent.putExtra()方法传出数据. ...
- BZOJ 1006 神奇的国度
Description K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系 ...
- 【Java】对Web Service的理解
WSDL(Web Service Description Language)是描述Web Service的语言. 你会怎样向别人介绍你的Web service有什么功能,以及每个函数调用时的参数呢?你 ...
- SIM卡基础知识
一:了解Sim卡和GSM网络登录步骤的基本知识 (一)名词解释: SIM卡(Subscriber Identity Module),即用户识别卡,它是一张符合GSM规范的“智慧卡”,SIM卡有大小之分 ...
- SpringMvc配置 导致实事务失效
SpringMVC回归MVC本质,简简单单的Restful式函数,没有任何基类之后,应该是传统Request-Response框架中最好用的了. Tips 1.事务失效的惨案 Spring MVC最打 ...
- qml学习:对象和属性
qml学习:对象和属性 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 参考文档<<Qt及Qt Quick开发实战精解.pdf>> ...