繁体   English   中英

ListView中的Android圆角

[英]Android rounded corners in ListView

我有一个带有圆角的 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>

问题出在选择器上。 它是矩形的,因此在选择第一个或最后一个项目时,角不再是圆角的。 我在http://www.anddev.org/view-layout-resource-problems-f27/rounded-corners-on-listview-t8193-15.html的上一篇文章中找到了一个非常好的解决方案。 问题是我不能让另一个 class 从 ListView 继承。 当我唯一拥有的是对现有 ListView 的引用时,如何应用此方法? 我必须这样做的原因是布局从 xml 膨胀。

我正在寻找类似的东西:

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

谢谢

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

您可以使用在背景中使用的 9 间距图像来实现它。 请在 android 网站上查看文档。

谢谢

您可以制作带有圆角的透明 9Patch 图像,并将其用作遮罩来覆盖您的 LinearLayout。 这样,选择器是否溢出并不重要 - 掩码将始终覆盖该问题。

我需要找到一种很好的方法来屏蔽我的任何布局,以创建典型的 iOS 风格的布局。

我写了一个相当完整的答案: Android XML 圆角

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM