如何创建一个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的分割线的更多相关文章

  1. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  2. android2.3 View视图框架源码分析之一:android是如何创建一个view的?

    View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类.只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已. ...

  3. 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)

    发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...

  4. android 自己创建一个凝视模板

    android  自己创建一个凝视模板 作为一名程序猿 不仅要有一个写代码的能力,养成一个良好的编写习惯也是非常重要的. 今天给大家具体介绍一下怎样创建凝视模板,给每一个类和方法都自己手动去凝视信息也 ...

  5. Android下创建一个输入法

    输入法是一种可以让用户输入文字的控件.Android提供了一套可扩展的输入法框架,使得应用程序可以让用户选择各种类型的输入法,比如基于触屏的键盘输入或者基于语音.当安装了特定输入法之后,用户即可在系统 ...

  6. 1.3、Android Studio创建一个Android Library

    一个Android Library结构上与Android app模块相同.它可以包含构建一个app需要的所有东西,包括圆满,资源文件和AndroidManifest.xml.然而,并非编译成运行在设备 ...

  7. 1.1、Android Studio创建一个项目

    Android Studio中的项目包含一个或多个模块.本节帮助你创建一个新的项目. 创建一个新的项目 如果你之前没有打开项目,Android Studio显示欢迎页面,通过点击Start a New ...

  8. [android] android下创建一个sqlite数据库

    Sqlite数据库是开源的c语言写的数据库,android和iphone都使用的这个,首先需要创建数据库,然后创建表和字段,android提供了一个api叫SQLiteOpenHelper数据库的打开 ...

  9. Android Wear创建一个通知

    创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了.(如果只是测试通知,则直接运行wear app就能够看到效果) 创建一个简单的wear通知分 ...

随机推荐

  1. 根据select不同的选项实现相应input框添加项的显示

    实现效果: @1.单击包时,显示包时的添加项 @2.单击包里程,显示包里程的添加项 二  代码实现: 给select添加change事件 获取当前select的value 根据value判断对象显示其 ...

  2. CSS让图片垂直居中的几种技巧 三种方法介绍

    在网页设计过程中,有时候会希望图片垂直居中的情况.而且,需要垂直居中的图片的高度也不确定,这就会给页面的布局带来一定的挑战.下面总结了一下,曾经使用过的几种方法来使图片垂直居中,除了第一种方法只限于标 ...

  3. pkg-config相关的常用指令

    pkg-config用途: 查询系统已安装库的基础信息(元信息) 1.查看所有的pkg-config库 pkg-config --list-all --list-all  列出pkg-config路径 ...

  4. Ajax 的同步与异步

    1.Ajax的工作原理如图: 2.同步 XMLHttpRequest 对象用于和服务器交换数据. XMLHttpRequest 对象如果要用于 AJAX 的话,其 open() 方法的 async 参 ...

  5. 会声会影X6-高级运动等效果的练习实践-与您分享...

          视频片说明:我在学习X6的视频教程后,做了针对性练习与实 践,我所用的素材取于网络世界-百度下载,视频中的效果有,高级运动;平移缩放,分屏效果,<运用:关键帧,缩放,旋转,加相框,倒 ...

  6. Facebook IV Winner's Interview: 1st place, Peter Best (aka fakeplastictrees)

    Facebook IV Winner's Interview: 1st place, Peter Best (aka fakeplastictrees) Peter Best (aka fakepla ...

  7. tomcat服务器报Server at localhost was unable to start within 45 seconds的问题

    今天在同一个tomcat服务器下部署了2个不同的应用程序,然后启动时报错:Server at localhost was unable to start within 45 seconds.If th ...

  8. Java实现Qt的SIGNAL-SLOT机制

    SIGNAL-SLOT是Qt的一大特色,使用起来十分方便.在传统的AWT和Swing编程中,我们都是为要在 监听的对象上添加Listener监听器.被监听对象中保存有Listener的列表,当相关事件 ...

  9. HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned

    参考: .Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is re ...

  10. centos 6.5 安装weixin

    下载cpanm wget http://xrl.us/cpanm --no-check-certificate -O /sbin/cpanm && chmod +x /sbin/cpa ...