ListView中的Android圆角
我有一个圆角的列表视图,使用以下形状作为背景:ListView中的Android圆角,android,listview,selector,rounded-corners,Android,Listview,Selector,Rounded Corners,我有一个圆角的列表视图,使用以下形状作为背景: <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ffffff"/> <corners android:bottomRightRadius="13px
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:bottomRightRadius="13px" android:bottomLeftRadius="13px" android:topLeftRadius="13px" android:topRightRadius="13px"/>
</shape>
谢谢您可以使用背景中使用的9音高图像来实现它。 请在android网站上查看文档
谢谢看来除了扩展ListView类并在XML中使用它之外别无选择。以下是示例代码:
public class WListView extends LinearLayout
{
// =================================================================
// Variables
// =================================================================
private Path clipArea;
// =================================================================
// Public methods
// =================================================================
public WListView(Context context)
{
super(context);
}
public WListView(Context context, AttributeSet attr)
{
super(context, attr);
}
// =================================================================
// Private methods
// =================================================================
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH)
{
super.onSizeChanged(w, h, oldW, oldH);
clipArea = new Path();
RectF rect = new RectF(0, 0, w, h);
int cornerRadius = 13; // we should convert px to dp here
clipArea.addRoundRect(rect, cornerRadius, cornerRadius, Path.Direction.CW);
}
@Override
protected void dispatchDraw(Canvas canvas)
{
canvas.save();
canvas.clipPath(clipArea);
super.dispatchDraw(canvas);
canvas.restore();
}
}
您可以制作一个带有圆角的透明面片图像,并将其用作遮罩来覆盖线性布局。这样,选择器的出血就无关紧要了——遮罩将始终覆盖该问题 我需要找到一种屏蔽任何布局的好方法来创建典型的iOS风格的布局 关于这个问题,我写了一个完整的答案:
您知道可以在布局XML文件中使用自定义布局对象吗?
public class WListView extends LinearLayout
{
// =================================================================
// Variables
// =================================================================
private Path clipArea;
// =================================================================
// Public methods
// =================================================================
public WListView(Context context)
{
super(context);
}
public WListView(Context context, AttributeSet attr)
{
super(context, attr);
}
// =================================================================
// Private methods
// =================================================================
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH)
{
super.onSizeChanged(w, h, oldW, oldH);
clipArea = new Path();
RectF rect = new RectF(0, 0, w, h);
int cornerRadius = 13; // we should convert px to dp here
clipArea.addRoundRect(rect, cornerRadius, cornerRadius, Path.Direction.CW);
}
@Override
protected void dispatchDraw(Canvas canvas)
{
canvas.save();
canvas.clipPath(clipArea);
super.dispatchDraw(canvas);
canvas.restore();
}
}