我要画一条虚线。我现在用这个来画实线:
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 ) );
我需要这样的东西,但不是实心的,而是虚线的。我想避免在透明布局和固体布局之间交替制作数百个布局。
类似于777,这里有一个水平线的解决方案:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-1dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#111"
android:dashWidth="8dp"
android:dashGap="2dp"
/>
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
线索是<item android:top="-1dp">。
为了在旧设备上显示虚线(<= API 21),你应该用android:layerType="software"创建一个视图(见android虚线可绘制潜在ICS错误):
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/dashed_line"
android:layerType="software"
/>
你也可以在没有android:layerType="software"的情况下添加相同的视图到layout-v23,以获得更好的性能,但我不确定它是否适用于所有具有API 23的设备。
我已经为EditText创建了虚线。给你。
创建新的xml。代码如下:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000000"
android:dashGap="3dp"
android:dashWidth="1dp" />
<solid android:color="#00FFFFFF" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item></layer-list>
并使用您的新xml文件在您的EditText例如:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dashed_border"/>
干杯!:)
我自定义了一个虚线,支持水平和垂直虚线。下面的代码:
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);
}
}
}
当我想要绘制虚线时,我所做的是定义一个可绘制的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" />