简体   繁体   English

android-以编程方式更改listview背景色

[英]android - programatically change listview background color

I created a custom listview and and created an adapter for it. 我创建了一个自定义列表视图,并为其创建了一个适配器。 I'd like to be able to programatically change different style aspects of different views within my custom listview. 我希望能够以编程方式在自定义列表视图中更改不同视图的不同样式方面。 My custom listview is a RelativeLayout that has TextViews inside of it. 我的自定义列表视图是其中包含TextViews的RelativeLayout。 i tried the following with no luck. 我没有运气尝试了以下内容。 No errors or anything. 没有错误或任何东西。

View myView = adapter.getView(position, null, null);

myView.setBackgroundColor(Color.parseColor("#93E6CD"));       
myView.findViewById(R.id.layout).setBackgroundColor(Color.parseColor("#93E6CD"));
adapter.notifyDataSetChanged();

here is my custom listview 这是我的自定义列表视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip"
    android:id="@+id/layout">


    <!-- Title-->
    <TextView
        android:id="@+id/primaryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="18dip"
        android:textStyle="bold"/>

    <!-- Secondary title -->
    <TextView
        android:id="@+id/secondaryTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/primaryTitle"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="1dip"
        android:text="" />

    <!-- Rightend Data -->
    <TextView
        android:id="@+id/rightData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/primaryTitle"
        android:gravity="right"
        android:text=""
        android:layout_marginRight="5dip"
        android:textSize="12dip"
        android:textColor="#B53021"
        android:textStyle="bold"/>

</RelativeLayout>

We won't be able to proceed until you provide more information. 在您提供更多信息之前,我们将无法继续。

I suspect you're not setting up your ListView correctly. 我怀疑您没有正确设置ListView。 Are you really trying to display a list of similar items, such as rows from a database? 您是否真的要显示类似项目的列表,例如数据库中的行? If so, I would like to see the definition of the ListView object, either in code or in XML. 如果是这样,我希望以代码或XML形式查看ListView对象的定义。 I'd also like to see the definition of adapter. 我也想看看适配器的定义。 That looks like you've defined a ListAdapter for the ListView, but I also want to know what is backing the ListAdapter. 看起来您已经为ListView定义了ListAdapter,但是我也想知道支持ListAdapter的内容。

The line 线

myView.findViewById(R.id.layout).setBackgroundColor()

looks suspicious. 看起来可疑。 You seem to be looking for a child view of myView, but I don't see where you've defined this child view. 您似乎在寻找myView的子视图,但是我看不到您在哪里定义了该子视图。

Usually, you define a ListView as part of your entire UI layout file, a separate item layout that defines one item in the ListView, and then adapt a data source to the ListView. 通常,您将ListView定义为整个UI布局文件的一部分,然后将其定义为ListView中一个项目的单独项目布局,然后将数据源调整为ListView。

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

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