Android自定义虚线

一、shape 样式:(在drawable新建--》new--》Drawable resource file 并把原父级标签selector改为shape )

 <!--只能画水平线,画不了竖线;-->
<!--线的高度是通过stroke的android:width属性设置的;-->
<!--size的android:height属性定义的是整个形状区域的高度;-->
<!--size的height必须大于stroke的width,否则,线无法显示;-->
<!--线在整个形状区域中是居中显示的;-->
<!--线左右两边会留有空白间距,线越粗,空白越大;-->

 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="5dp"
        android:color="#4ec5ff"
        android:dashGap="2dp"
        android:dashWidth="10dp" />
</shape>
二、style 样式:

<style name="line">

    <item name="android:background">@drawable/buttonlinestyle</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">40dp</item>
</style>
三、Button控件调用style样式:

 <!--引用虚线的view需要添加属性android:layerType,值设为"software",否则显示不了虚线。-->

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ly.blogtest.MainActivity">

 
<LinearLayout
    style="@style/line"
    android:id="@+id/linearLayout"
    android:orientation="horizontal"
   android:layerType="software"
   >

</LinearLayout>
 
</RelativeLayout>

----------------------------------View虚线或者直线-----------------------------------------------------

 ---------------------------------画水平线、虚线注意-----------------------------------------------

  1. 只能画水平线,画不了竖线;
  2. 线的高度是通过stroke的android:width属性设置的;
  3. size的android:height属性定义的是整个形状区域的高度;
  4. size的height必须大于stroke的width,否则,线无法显示;
  5. 线在整个形状区域中是居中显示的;
  6. 线左右两边会留有空白间距,线越粗,空白越大;
  7. 引用虚线的view需要添加属性android:layerType,值设为"software",否则显示不了虚线。

---------------------------------划线注意-----------------------------------------------

----------------------------------android:shape属性指定形状------------------------------

  • rectangle: 矩形,默认的形状,可以画出直角矩形、圆角矩形、弧形等
  • oval: 椭圆形,用得比较多的是画正圆
  • line: 线形,可以画实线和虚线
  • ring: 环形,可以画环形进度条

----------------------------------android:shape属性指定形状------------------------------

----------------------------------shape的属性标签-----------------------------------------

  <shape>
            <!-- 实心 -->
            <solid android:color="#ff9d77"/>
            <!-- 渐变 -->
            <gradient
                android:startColor="#ff8c00"
                android:endColor="#FFFFFF"
                android:angle="270" />
            <!-- 描边 -->
            <stroke
                android:width="2dp"
                android:color="#dcdcdc" />
            <!-- 圆角 -->
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>

  • solid: 设置形状填充的颜色,只有android:color一个属性

    • android:color 填充的颜色
  • padding: 设置内容与形状边界的内间距,可分别设置左右上下的距离

    • android:left 左内间距
    • android:right 右内间距
    • android:top 上内间距
    • android:bottom 下内间距
  • gradient: 设置形状的渐变颜色,可以是线性渐变、辐射渐变、扫描性渐变

    • android:type 渐变的类型
      • linear 线性渐变,默认的渐变类型
      • radial 放射渐变,设置该项时,android:gradientRadius也必须设置
      • sweep 扫描性渐变
    • android:startColor 渐变开始的颜色
    • android:endColor 渐变结束的颜色
    • android:centerColor 渐变中间的颜色
    • android:angle 渐变的角度,线性渐变时才有效,必须是45的倍数,0表示从左到右,90表示从下到上
    • android:centerX 渐变中心的相对X坐标,放射渐变时才有效,在0.0到1.0之间,默认为0.5,表示在正中间
    • android:centerY 渐变中心的相对X坐标,放射渐变时才有效,在0.0到1.0之间,默认为0.5,表示在正中间
    • android:gradientRadius 渐变的半径,只有渐变类型为radial时才使用
    • android:useLevel 如果为true,则可在LevelListDrawable中使用
  • corners: 设置圆角,只适用于rectangle类型,可分别设置四个角不同半径的圆角,当设置的圆角半径很大时,比如200dp,就可变成弧形边了

    • android:radius 圆角半径,会被下面每个特定的圆角属性重写
    • android:topLeftRadius 左上角的半径
    • android:topRightRadius 右上角的半径
    • android:bottomLeftRadius 左下角的半径
    • android:bottomRightRadius 右下角的半径
  • stroke: 设置描边,可描成实线或虚线。

    • android:color 描边的颜色
    • android:width 描边的宽度
    • android:dashWidth 设置虚线时的横线长度
    • android:dashGap 设置虚线时的横线之间的距离

----------------------------------shape的属性标签-----------------------------------------

原文地址:https://www.cnblogs.com/LOVEJIEYING/p/6016591.html

猜你喜欢

转载自blog.csdn.net/freak_csh/article/details/81285344
今日推荐