gpt4 book ai didi

ListView中的Android圆角

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:23 25 4
gpt4 key购买 nike

我有一个带圆角的 ListView,使用以下形状作为背景:

<?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>

问题出在选择器上。 It's rectangle shaped, so when selecting first or last item the corners aren't rounded anymore.我在 http://www.anddev.org/view-layout-resource-problems-f27/rounded-corners-on-listview-t8193-15.html 的最后一篇文章中找到了一个非常好的解决方案。 .问题是我不能让另一个类继承自 ListView。当我唯一拥有的是对现有 ListView 的引用时,如何应用此方法?我必须这样做的原因是布局是从 xml 扩展而来的。

我正在寻找类似的东西:

ListView lv = (ListView)findViewById(...);
lv.onSizeChanged = protected void onSizeChanged(int w, int h, int oldw, int oldh){ ... }

谢谢

最佳答案

看起来除了扩展 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();
}
}

关于ListView中的Android圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6761907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com