画一条虚线

方法一:

1、

通过自定义控件

<LinearLayout 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:orientation="vertical" >
    <com.example.drawxuxian.DotView
        android:layout_marginTop="10dp"
        android:layout_height="2dp"
        android:layout_width="match_parent">
    </com.example.drawxuxian.DotView>
</LinearLayout>

2、

复制进自己项目的时候注意上面xml中自定义控件名称

public class DotView extends View {
private Paint p;
private int width;
private int height;
private int dash;
public DotView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DotView(Context context) {
super(context);
init(context);
}
private void init(Context context){
p = new Paint(Paint.ANTI_ALIAS_FLAG);  
p.setStyle(Style.FILL);  
p.setColor(Color.parseColor("#d8d8d8")); 

final float scale = context.getResources().getDisplayMetrics().density;
dash=(int) (6 * scale + 0.5f);
}
@Override
protected void onDraw(Canvas canvas) {
if(width>10){
for(int i=0;i<width;i+=dash){
canvas.drawLine(i, 0, i+=dash, 0, p);
}
}
super.onDraw(canvas);
}

@Override  
        public void onSizeChanged(int w, int h, int oldw, int oldh) {  
          super.onSizeChanged(w, h, oldw, oldh); 
          width=w;
          height=h;
          p.setStrokeWidth(height); 
          this.postInvalidate();
    } 
}


方法二:

1、

在drawable目录下,定义一个line.xml文件
<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"   
     android:shape="line">  
     <!-- 显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线 -->  
   <stroke android:width="1dp" android:color="#D5D5D5"      
             android:dashWidth="2dp" android:dashGap="3dp" />     
             <!-- 虚线的高度 -->   
     <size android:height="2dp" />      
</shape>   

2、

在布局文件中定义一个view

<View
        android:layout_marginTop="10dp"
        android:layout_height="2dp"
        android:layout_width="match_parent"
        android:background="@drawable/line"
        />

3、

关闭硬件加速:(这句代码一定要有)
<activity
XXX
            android:hardwareAccelerated="false"
XXX         
 </activity>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值