Android ArrayAdapter 背景图片

发布于 2024-11-04 11:24:50 字数 3956 浏览 2 评论 0原文

我想为我的列表视图设置背景图像,以便无论您位于列表中的哪个位置,您都会看到相同的背景图像。

对于滚动视图,这只是在 XML 属性中设置滚动视图的背景图像的情况,但对列表视图执行同样的操作不起作用

我正在使用自定义 ArrayAdapter 如下:

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;
    }


}

然后

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);

是我的 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>

还有

<?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>
        <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>

我的样式

<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>

为每个 TableRow 设置样式只会使每个 TableRow 使用整个背景图像,这不是我想要的。

另外可能有一种更好的方法来实现我的 ArrayAdapter 但我不热衷于更改它,除非没有其他方法可以达到我想要的效果

这已经困扰我一段时间了,所以任何帮助将不胜感激,

谢谢 勺子拇指

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>
        <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-11-11 11:24:50

通过使其透明来禁用默认的 ListView 缓存颜色提示优化:

<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>

请参阅 http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文