根据某些条件,我必须对角剪切列表单元格。为此,我使用以下代码制作了对角线可绘制的图像:
对角线.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:top="0dp"
android:bottom="0dp"
>
<rotate
android:fromDegrees="315"
android:toDegrees="315"
android:pivotX="0%"
android:pivotY="0%" >
<shape
android:shape="line"
>
<stroke
android:width="10dp"
android:color="@color/grey" />
</shape>
</rotate>
</item>
</layer-list>
在列表单元格的xml中,它被用作:
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/diagonal_line"
android:layerType="software"
android:scaleType="fitXY"
/>
这条对角线出现在单元格xml的图形视图中,但在列表膨胀后不会出现。目前,它的可见性与任何条件无关,即可见性始终为真。
知道问题在哪里吗?
对角线的自定义视图为:
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DiagonalLineView extends View {
private int dividerColor;
private Paint paint;
public DiagonalLineView(Context context)
{
super(context);
init(context);
}
public DiagonalLineView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public DiagonalLineView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
Resources resources = context.getResources();
dividerColor = resources.getColor(R.color.grey);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(dividerColor);
paint.setStrokeWidth(resources.getDimension(R.dimen.vertical_divider_width));
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
}
}
它对我有效。