Android ArrayAdapter Background Image
I want to set a background image to my listview so that regardless of where you are in the list, you see the same background image.
For scrollview, this is just a case of setting the background image of the scrollview in the XML attributes, but doing likewise for the listview doesn't work
I'm using a custom ArrayAdapter as such:
private class CustomAdapter extends ArrayAdapter<TableRow>{
private ArrayList<TableRow> items;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<TableRow> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_season, null);
}
TableRow o = items.get(position);
if (o != null) {
v = o;
}
return v;
}
}
and then
ListView lv = new ListView(this);
lv.setId(R.id.list_listView1);
ArrayList<TableRow> tablerows = new ArrayList<TableRow>();
// create some TableRow's and add them to tablerows
CustomAdapter vAdapt = new CustomAdapter(this, R.id.list_season_item_name,tablerows);
lv.setAdapter(vAdapt);
setContentView(lv);
and my xml:
<ListView
android:id="@+id/list_listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/standard_layout">
<LinearLayout
android:id="@+id/list_linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/standard_layout">
<TextView
android:text="list_textview1"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
and also
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_alignParentLeft="true"
android:id="@+id/list_season_row"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="10dip"
android:paddingLeft="15dip"
android:paddingBottom="10dip">
<TextView
android:layout_weight="3"
android:layout_width="0dip"
android:paddingRight="10dip"
android:id="@+id/list_season_item_name"
android:layout_height="wrap_content"
style="@style/standard_item">
</TextView>开发者_JAVA百科
<TextView
android:paddingLeft="10dip"
android:layout_weight="1"
android:id="@+id/list_season_item_season"
android:gravity="left"
android:layout_gravity="right"
android:layout_width="0dip"
android:layout_height="wrap_content"
style="@style/standard_season">
</TextView>
</TableRow>
and my styles
<style name="standard_item" parent="@style/standard_font">
<item name="android:textSize">18sp</item>
</style>
<style name="standard_season" parent="@style/standard_font">
<item name="android:textSize">12sp</item>
</style>
<style name="standard_layout" parent="@style/standard_font">
<item name="android:background">@drawable/background_img</item>
</style>
Setting a style for each TableRow just makes each one use the whole background image, which is not what I'm after.
Also there is probably a better way to implement my ArrayAdapter but I'm not keen on changing it unless there is no other way to get the effect I'm after
This has been bugging me for a while, so any help would be appreciated,
Thanks Spoon Thumb
Disable the default ListView cache color hint optimization by making it transparent:
<style name="standard_layout" parent="@style/standard_font">
<item name="android:background">@drawable/background_img</item>
<item name="android:cacheColorHint">@android:color/transparent</item>
</style>
See http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html
精彩评论