zoukankan      html  css  js  c++  java
  • [Android]如何创建一个View的分割线

    buttons-with-separators3 

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

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

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

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

    buttons-with-separators3 

    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

  • 相关阅读:
    MySQL根据父ID排序类别
    isNotBlank的用法
    IDEA中的.iml文件和.idea文件夹
    java组件:获取查询月份的第一天和最后一天,默认取当前月份
    MyBatis:条件构造器QueryWrapper方法详解
    querywrapper条件构造器
    mybatisplus主键策略选择
    mybatis-plus 高级搜索分页功能的实现 sql语句 QueryWrapper 条件判断
    QueryWrapper查询
    MySQL根据父ID排序类别(mysql sort category according to parent id)
  • 原文地址:https://www.cnblogs.com/dongweiq/p/3928718.html
Copyright © 2011-2022 走看看