Android: Custom ListView Drawing
Consider me an Android noob; I'm trying to create a custom ListView, that should look something like this ('this' is a custom ListView implemented in BlackBerry, but I want to create the same look and feel on Android):
I've currently come up with the following Android code and XML, but it doesn't change the looks of the standard ListView:
The code:
public class RoundedListView extends ListView
{
public RoundedListView(Context context)开发者_如何学Go {
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);
}
}
The 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>
Does anyone know how to make this simple drawing function working? Thanks in advance!
Android is quite different from Blackberry.
In Android you would want to set backgrounds and margins on the items of the ListView
to achieve the above effects. There is usually no need to combine custom drawing with "normal" members of the view hierarchy like ListView
, TextView
, etc.
Often in Blackberry development you would subclass a framework field just to provide it with a custom width and height. In Android you can do this with simple "composition"; in other words, instantiating a component and then setting attributes on it without subclassing.
Another important point is that ListView
tends to be for homogeneous data, where all of the rows follow the same general format or a small group of formats. If you only have four items, and each of the items have different characteristics, then I suggest you use a plain old LinearLayout
which will allow you to position your views vertically. This will save you quite a bit of work shoe-horning your four views into an adapter and give you maximum control over how each item is displayed.
Here's another couple of places you can start looking around for this simple layout:
http://www.droiddraw.org/widgetguide.html
http://developer.android.com/resources/articles/listview-backgrounds.html
All this seems to be is a couple of edittexts and buttons within a listview.
精彩评论