在Android中,如何在不禁用onClick颜色的情况下更改listView的背景颜色?(In Android how do you change the background color of a

编程入门 行业动态 更新时间:2024-10-08 04:31:00
在Android中,如何在不禁用onClick颜色的情况下更改listView的背景颜色?(In Android how do you change the background color of a listView without disabling the onClick color?)

我有一个listView,我需要提供背景颜色来掩盖活动的背景图像。 我希望在单元格的基础上执行此操作,以便如果listView没有到达屏幕的底部,则背景颜色将显示没有更多列表项目要显示的位置。 另外,我应该提一下,我将这个ListView放在ImageViews之间,使用:

android:layout_above

android:layout_below

因此,对于较小的屏幕,ListView将在较低的ImageView顶部正确滚动,而不是在它的前面或后面。 但是对于较大的屏幕,我希望当没有更多的列表项显示时,活动的背景图像可见。

我一直在寻找选择器,这可能是我正在寻找的东西,但据我所知,选择器的颜色将显示在listView的背景颜色后面。

这就是我现在正在尝试的。 关于如何完成我想要的任何建议?

item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:state_focused="true" android:drawable="@drawable/orange" /> <item android:state_pressed="true" android:drawable="@drawable/orange" /> <item android:state_focused="true" android:drawable="@drawable/orange" /> </selector>

下面的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gray_lines" > <ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="45dp" android:scaleType="fitXY" android:src="@drawable/logo_header" /> <!-- Footer aligned to bottom --> <ImageView android:id="@+id/footer" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:scaleType="fitXY" android:src="@drawable/orange" /> <TextView android:id="@+id/footerText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="5dp" android:layout_marginLeft="14dp" android:onClick="contactPressed" android:listSelector="@drawable/item" android:drawSelectorOnTop="true" android:text="@string/footerText" /> <ListView android:id="@+id/mainMenuList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/header" android:layout_above="@id/footer" android:divider="#000000" android:dividerHeight="0.1dp" /> </RelativeLayout>

这是我的list_item.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gray" > <ImageView android:id="@+id/img" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginRight="15dp" android:layout_marginTop="20dp" android:layout_alignParentRight="true" /> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginTop="17dp" android:layout_marginLeft="15dp" android:textSize="23sp" /> </RelativeLayout&t;

这将创建正确的颜色,如下所示,但onClick颜色不会显示它应该。

I have a listView that I need to give a background color to cover up the activity's background image. I would prefer to do this on a cell basis so that if the listView doesn't reach the bottom of the screen, the background color will show where there are no more list items to display. Also, I should mention that I have this ListView positioned between to ImageViews using:

android:layout_above

and

android:layout_below

So that for smaller screens, the ListView will scroll properly on top of the lower ImageView rather than in front of or behind it. For larger screens however, I'd like the activity's background image to be visible when there are no more list items to display.

I've been looking around at selectors and that might be what I'm looking for, but from what I'm told the selector's color will display behind the background color for the listView.

This is what I'm trying now. Any suggestions on how to accomplish what I'm trying to?

item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:state_focused="true" android:drawable="@drawable/orange" /> <item android:state_pressed="true" android:drawable="@drawable/orange" /> <item android:state_focused="true" android:drawable="@drawable/orange" /> </selector>

activity_main.xml below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gray_lines" > <ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="45dp" android:scaleType="fitXY" android:src="@drawable/logo_header" /> <!-- Footer aligned to bottom --> <ImageView android:id="@+id/footer" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:scaleType="fitXY" android:src="@drawable/orange" /> <TextView android:id="@+id/footerText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="5dp" android:layout_marginLeft="14dp" android:onClick="contactPressed" android:listSelector="@drawable/item" android:drawSelectorOnTop="true" android:text="@string/footerText" /> <ListView android:id="@+id/mainMenuList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/header" android:layout_above="@id/footer" android:divider="#000000" android:dividerHeight="0.1dp" /> </RelativeLayout>

and this is my list_item.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gray" > <ImageView android:id="@+id/img" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginRight="15dp" android:layout_marginTop="20dp" android:layout_alignParentRight="true" /> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginTop="17dp" android:layout_marginLeft="15dp" android:textSize="23sp" /> </RelativeLayout>

Which creates the correct colors as shown below, but the onClick color is not displaying as it should.

最满意答案

如果您只想为列表视图设置静态颜色,请将其添加到xml中的listview元素

<ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background = "@drawable/somebgdrawable" />

如果要将drawable添加到单元格中

然后在你的list_item.xml

将背景线添加到布局的根视图中

如果你的drawable是一个选择器,那么添加它的识别为@drawable

如果您正在使用列表活动

getListView().setBackground(Drawable d)

如果你在单元格的基础上执行此操作 - >单元格将被覆盖,但如果列表未到达屏幕底部,则活动将在其后面显示(如果列表视图是叠加层)

或者你将在其余部分获得一段时间的屏幕

The answer to my question is silly. I had the selector located in the textview instead of the ListView. Right things wrong spot.

<TextView android:id="@+id/footerText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="5dp" android:layout_marginLeft="14dp" android:onClick="contactPressed" android:text="@string/footerText" /> <ListView android:id="@+id/mainMenuList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/header" android:layout_above="@id/footer" android:listSelector="@drawable/item" android:drawSelectorOnTop="true" android:divider="#000000" android:dividerHeight="0.1dp" />

更多推荐

本文发布于:2023-07-30 15:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   情况下   背景   如何在   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!