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

如何创建一个View的分割线,如上图

我们见介绍三种可以创建看起来很不错的view的分割线,如在button之间添加分割线。

这个例子是将为LinearLayout内的三个Button间添加分割线。

这三个例子可能容易实现,相信会有更好的实现办法。

1 人工添加LinearLayout的分割线

我们可以创建一个View,这个View就是分割线,只要简单在Button之间添加这个分割线就可以。

分割线的实现,如下:

<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:

<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版本使用。

我们先定义这个分割线样式吧:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <size android:width="1dp" />
 <solid android:color="#90909090" />
</shape>

  

把这个分割线的样式设置给LinearLayout:

<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:

<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的分割线,布布扣,bubuko.com

时间: 2024-07-30 10:12:08

[Android]如何创建一个View的分割线的相关文章

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

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

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

View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类.只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已. 这一篇文章把所有的layout和widget都统称为view,那么android是如何创建一个view的呢? 一.在代码中直接new出来. 比如说你要创建一个TextView的实例,那么你可以这样写: Java代码   TextView text = new TextView(c);  //c为c

《android 1: 创建一个安卓项目》

创建方式有两种: 通过Eclipse创建 在工具栏上选择New>android>android application project,或者在导航栏上选择file>new>project>android>android application project. 在出现的窗口上填上与选择你的 Application Name .Project Name.Package Name.Minimum Required SDK.Target SDK.Compile With.Th

android 自己创建一个注释模板

android  自己创建一个注释模板 作为一名程序员 不仅要有一个写代码的能力,养成一个良好的编写习惯也是很重要的.今天给大家详细介绍一下如何创建注释模板,给每个类和方法都自己手动去注释信息也可以,只是这样比较繁琐.为何我们不手动去创建一个注释模板呢? 第一: 首先我们需要编写一个模板的xml文件(codetemplates.xml) 第二.我们现在就给xml文件编写主要注释的内容信息,我就直接把自己用的xml信息直接贴出来 <?xml version="1.0" encodi

android 自己创建一个凝视模板

android  自己创建一个凝视模板 作为一名程序猿 不仅要有一个写代码的能力,养成一个良好的编写习惯也是非常重要的. 今天给大家具体介绍一下怎样创建凝视模板,给每一个类和方法都自己手动去凝视信息也能够.仅仅是这样比較繁琐.为何我们不手动去创建一个凝视模板呢? 第一: 首先我们须要编写一个模板的xml文件(codetemplates.xml) 第二.我们如今就给xml文件编写主要凝视的内容信息,我就直接把自己用的xml信息直接贴出来 <? xml version="1.0" e

Android下创建一个输入法

输入法是一种可以让用户输入文字的控件.Android提供了一套可扩展的输入法框架,使得应用程序可以让用户选择各种类型的输入法,比如基于触屏的键盘输入或者基于语音.当安装了特定输入法之后,用户即可在系统设置中选择个输入法,并在接下来的输入场景中使用该输入法.不过在任一时刻,只能使用一个输入法. 为了在安卓系统下创建一个输入法,需要新建一个包含扩展了InputMethodService类的安卓应用,并创建一个用于设置的activity,用户可以通过它将设置选项传给输入法的service,因此,你还需

Android Wear创建一个通知

创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了.(如果只是测试通知,则直接运行wear app就能够看到效果) 创建一个简单的wear通知分为3步: 一.创建一个Intent用于设置你要做的动作 二.创建一个PendingIntent把Intent放进去(主要是根据intent传入的内容做跳转动作) 三.创建一个NotificationCompat.Builder用于设置通知内容,例如:将PendingIntent传递进去用于actio

Android中创建一个BroadcastReceiver

首先创建一个java类继承BroadcastReceiver类 package com.example.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastRecei

Android下创建一个SQLite数据库

数据库:SQLite(轻量级,嵌入式的数据库) 大量的相似结构的数据的储存,快速的查询.特殊的文件(按照一定的格式生成) 数据库的创建 创建文件 1.声明文件对象,文件是不会被创建出来的. File file = new File("文件名称"); 2.写文件(文件才会被创建出来) FileOutputStream fos = new FileOutputStream(file); fos.write("hdahfdsaklfh".getbytes()); 创建数据