问题描述
我用下面的形状为背景制作带圆角的ListView控件:
I have a ListView with rounded corners made using following shape as background:
<?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>
问题在于选择。它是长方形状,所以选择第一个或最后一个项目时,边角不再四舍五入。我发现在过去的职位一个很好的解决方案,在http://www.anddev.org/view-layout-resource-problems-f27/rounded-corners-on-listview-t8193-15.html.问题是我不能让另一个类从ListView的继承。我如何应用此方法时,我唯一拥有的是参考现有的ListView?我之所以要做这种方式是布局从XML膨胀。
The problem lies in the selector. It's rectangle shaped, so when selecting first or last item the corners aren't rounded anymore. I've found a very nice solution in the last post at http://www.anddev.org/view-layout-resource-problems-f27/rounded-corners-on-listview-t8193-15.html. The problem is I can't make another class to inherit from ListView. How can I apply this method when the only thing I have is the reference to existing ListView? The reason I have to do it this way is that the layout is inflated from xml.
我在寻找这样的:
ListView lv = (ListView)findViewById(...);
lv.onSizeChanged = protected void onSizeChanged(int w, int h, int oldw, int oldh){ ... }
感谢
推荐答案
看起来像有比扩展的ListView类和XML使用它没有别的办法。这里的样品code:
Looks like there's no other way than extending the ListView class and using it in XML. Here's the sample code:
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();
}
}
这篇关于在ListView的机器人圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!