如何在Android中创build虚线/虚线?

我试图做一个虚线。 我现在正在使用这个实线:

LinearLayout divider = new LinearLayout( this ); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, 2 ); divider.setLayoutParams( params ); divider.setBackgroundColor( getResources().getColor( R.color.grey ) ); 

我需要这样的东西,但点缀而不是坚实。 我想避免使数百个布局在透明布局和实体布局之间交替。

没有java代码:

绘制/ dotted.xml:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:color="#C7B299" android:dashWidth="10px" android:dashGap="10px" android:width="1dp"/> </shape> 

view.xml用:

 <ImageView android:layout_width="match_parent" android:layout_height="5dp" android:src="@drawable/dotted" android:layerType="software" /> 

path效果设置在绘制对象上

 Paint fgPaintSel = new Paint(); fgPaintSel.setARGB(255, 0, 0,0); fgPaintSel.setStyle(Style.STROKE); fgPaintSel.setPathEffect(new DashPathEffect(new float[] {10,20}, 0)); 

你可以通过在int []数组中提供更多的数字来创build各种各样的虚线模式,它指定了虚线和间隙的比例。 这是一条简单的,同样破灭的路线。

这将帮助你。 使用XML创build虚线。 在drawable文件夹中创buildxml,并将该背景设置为要设置虚线边框的项目。

—->创buildXML背景“dashed_border”

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape> <solid android:color="#ffffff" /> <stroke android:dashGap="5dp" android:dashWidth="5dp" android:width="1dp" android:color="#0000FF" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape> </item> </layer-list> 

—–>将该背景添加到项目

 <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dashed_border"/> 

创buildxml(view_line_dotted.xml):

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:bottom="-1dp" android:left="-1dp" android:right="-1dp" android:top="0dp"> <shape android:shape="rectangle"> <stroke android:width="1dp" android:color="#ffff0017" android:dashGap="3dp" android:dashWidth="1dp" /> <solid android:color="@android:color/transparent" /> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape> </item> </layer-list> 

设置为您的视图的背景:

 <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/view_line_dotted" /> 

当我想画一条虚线时,我所做的是定义一个可绘制的dash_line.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <stroke android:dashGap="3dp" android:dashWidth="2dp" android:width="1dp" android:color="@color/black" /> </shape> 

然后在布局中定义一个背景为dash_line的视图。 注意包括android:layerType =“software” ,否则不起作用。

 <View android:layout_width="match_parent" android:layout_height="5dp" android:background="@drawable/dash_line" android:layerType="software" /> 

我定制了一个支持水平和垂直虚线的虚线。 代码如下:

 public class DashedLineView extends View { private float density; private Paint paint; private Path path; private PathEffect effects; public DashedLineView(Context context) { super(context); init(context); } public DashedLineView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public DashedLineView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { density = DisplayUtil.getDisplayDensity(context); paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(density * 4); //set your own color paint.setColor(context.getResources().getColor(R.color.XXX)); path = new Path(); //array is ON and OFF distances in px (4px line then 2px space) effects = new DashPathEffect(new float[] { 4, 2, 4, 2 }, 0); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); paint.setPathEffect(effects); int measuredHeight = getMeasuredHeight(); int measuredWidth = getMeasuredWidth(); if (measuredHeight <= measuredWidth) { // horizontal path.moveTo(0, 0); path.lineTo(measuredWidth, 0); canvas.drawPath(path, paint); } else { // vertical path.moveTo(0, 0); path.lineTo(0, measuredHeight); canvas.drawPath(path, paint); } } } 

我已经使用下面作为布局的背景:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:dashWidth="10px" android:dashGap="10px" android:color="android:@color/black" /> </shape> 

通过使用这个类,您可以将“虚线和下划线”效果应用于多行文本。 要使用DashPathEffect你必须closures你的TextView的hardwareAccelerated(虽然DashPathEffect方法有长文本的问题)。 你可以在这里find我的示例项目: https : //github.com/jintoga/Dashed-Underlined-TextView/blob/master/Untitled.png 。

公共类DashedUnderlineSpan实现LineBackgroundSpan,LineHeightSpan {

 private Paint paint; private TextView textView; private float offsetY; private float spacingExtra; public DashedUnderlineSpan(TextView textView, int color, float thickness, float dashPath, float offsetY, float spacingExtra) { this.paint = new Paint(); this.paint.setColor(color); this.paint.setStyle(Paint.Style.STROKE); this.paint.setPathEffect(new DashPathEffect(new float[] { dashPath, dashPath }, 0)); this.paint.setStrokeWidth(thickness); this.textView = textView; this.offsetY = offsetY; this.spacingExtra = spacingExtra; } @Override public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) { fm.ascent -= spacingExtra; fm.top -= spacingExtra; fm.descent += spacingExtra; fm.bottom += spacingExtra; } @Override public void drawBackground(Canvas canvas, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) { int lineNum = textView.getLineCount(); for (int i = 0; i < lineNum; i++) { Layout layout = textView.getLayout(); canvas.drawLine(layout.getLineLeft(i), layout.getLineBottom(i) - spacingExtra + offsetY, layout.getLineRight(i), layout.getLineBottom(i) - spacingExtra + offsetY, this.paint); } } 

}

结果

使用ShapeDrawable而不是LinearLayout,并使用dashWidth和dashGap进行操作

http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

我不知道为什么,但投票答案不适合我。 我这样写,工作很好。
定义一个自定义视图:

 public class XDashedLineView extends View { private Paint mPaint; private Path mPath; private int vWidth; private int vHeight; public XDashedLineView(Context context) { super(context); init(); } public XDashedLineView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public XDashedLineView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(Color.parseColor("#3F577C")); mPaint.setStyle(Paint.Style.STROKE); mPaint.setPathEffect(new DashPathEffect(new float[] {10,10}, 0)); mPath = new Path(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); this.vWidth = getMeasuredWidth(); this.vHeight = getMeasuredHeight(); mPath.moveTo(0, this.vHeight / 2); mPath.quadTo(this.vWidth / 2, this.vHeight / 2, this.vWidth, this.vHeight / 2); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(mPath, mPaint); } } 

那么你可以在你的xml中使用它:

  <com.YOUR_PACKAGE_NAME.XDashedLineView android:layout_width="690dp" android:layout_height="1dp" android:layout_marginLeft="30dp" android:layout_marginTop="620dp"/> 

我已经创build了一个自定义视图库来解决这个问题,它应该是非常简单的使用。 有关更多信息,请参阅https://github.com/Comcast/DahDit 。 你可以像这样添加虚线:

 <com.xfinity.dahdit.DashedLine android:layout_width="250dp" android:layout_height="wrap_content" app:dashHeight="4dp" app:dashLength="8dp" app:minimumDashGap="3dp" app:layout_constraintRight_toRightOf="parent" android:id="@+id/horizontal_dashes"/>