Android:自定义ListView绘图

时间:2011-03-30 17:59:25

标签: android listview layout drawing

考虑我一个Android noob; 我正在尝试创建一个自定义ListView,看起来应该是这样的('this'是在BlackBerry中实现的自定义ListView,但我想在Android上创建相同的外观):Custom ListView with rounded corners, implemented on BlackBerry

我目前提出了以下Android代码和XML,但它并没有改变标准ListView的外观:

代码:

public class RoundedListView extends ListView
{
    public RoundedListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public RoundedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public RoundedListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public void onDraw(Canvas canvas)
    {       
        Paint paint = new Paint();
        paint.setColor(Color.CYAN);

        canvas.drawRect(10, 10, 10, 10, paint);
        canvas.drawColor(Color.YELLOW);

        super.onDraw(canvas);
    }
}

XML(main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.gravityzoo.android.containers.RoundedListView 
        android:layout_height="wrap_content" 
        android:id="@+id/listView1" 
        android:layout_width="match_parent">
    </com.gravityzoo.android.containers.RoundedListView>
</LinearLayout>

有谁知道如何使这个简单的绘图功能工作? 提前谢谢!

2 个答案:

答案 0 :(得分:2)

Android与Blackberry完全不同。

在Android中,您需要在ListView的项目上设置背景和边距以实现上述效果。通常不需要将自定义绘图与视图层次结构的“普通”成员组合,如ListViewTextView等。

通常在Blackberry开发中,您可以将框架字段子类化,以便为其提供自定义宽度和高度。在Android中,您可以使用简单的“组合”来完成此操作;换句话说,实例化一个组件,然后在没有子类化的情况下在其上设置属性。

另一个重要的一点是ListView往往是同质数据,其中所有行都遵循相同的通用格式或一小组格式。 如果您只有四个项目,并且每个项目都有不同的特征,那么我建议您使用普通的LinearLayout ,这样您就可以垂直定位视图。这将为您节省相当多的工作量 - 将您的四个视图转换为适配器,并让您最大限度地控制每个项目的显示方式。

答案 1 :(得分:0)

这是另外几个你可以开始寻找这个简单布局的地方:

http://www.droiddraw.org/widgetguide.html

http://developer.android.com/resources/articles/listview-backgrounds.html

所有这些似乎都是列表视图中的几个edittexts和按钮。