繁体   English   中英

Android:自定义ListView绘图

[英]Android: Custom ListView Drawing

考虑我一个Android noob; 我正在尝试创建一个自定义ListView,看起来应该是这样的('this'是在BlackBerry中实现的自定义ListView,但我想在Android上创建相同的外观和感觉): 带有圆角的自定义ListView,在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>

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

Android与Blackberry完全不同。

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

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

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

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

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

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

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

暂无
暂无

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

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