[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通知分 ...
随机推荐
- 谈谈 jQuery 中的防冲突(noConflict)机制
许多的 JS 框架类库都选择使用 $ 符号作为函数或变量名,jQuery 是其中最为典型的一个.在 jQuery 中,$ 符号只是 window.jQuery 对象的一个引用,因此即使 $ 被删除,w ...
- 知识库总结mysql常用cmd命令
打开命令目录 打开D盘mysql目录 d: cd D:\Ampps\mysql\bin 常用操作 将mysql目录下bin目录中的mysql.exe放到C:\WINDOWS下,可以执行以下命令 连接: ...
- sys.argv[]用法
#-*- coding: utf-8 -*- """ sys.argv 用来获取命令行参数 sys.argv[0] 表示当前执行文件 "-k".sta ...
- 连接MySQL数据常见问题
错误信息1 :ERROR 1045 (28000): Access denied for user 'usera'@'localhost' (using password:YES) 错误信息2 :ER ...
- vs2013update4 vs-mda-remote cordova真机测试ios 解决里面一个坑
sudo npm install -g vs-mda-remote --user=你的用户名 此步骤为安装vs-mda-remote,如果安装成功 执行vs-mda-remote –secure fa ...
- mapreduce (四) MapReduce实现Grep+sort
1.txt dong xi cheng xi dong cheng wo ai beijing tian an men qiche dong dong dong 2.txt dong xi cheng ...
- matlab 全部的随机数函数
matlab 全部的随机数函数 (一)Matlab内部函数 a. 基本随机数 Matlab中有两个最基本生成随机数的函数. 1.rand() 生成(0,1)区间上均匀分布的随机变量.基本语法: ran ...
- Artem and Array
Codeforces Round #253 (Div. 1) C:http://codeforces.com/problemset/problem/442/C 题意:给你一个序列,然后你每次可以删除一 ...
- 微控制器(MCU)架构介绍
微控制器(MicroController)又可简称MCU或μC,也有人称为单芯片微控制器(Single Chip Microcontroller),将ROM.RAM.CPU.I/O集合在同一个芯片中, ...
- dt dd 如何在同一行上
<style> dl{clear:left;} dt,dd{float:left;} </style> <dl> <dt>test </dt> ...