具有九个补丁项目背景问题的 ListView

发布于 2024-10-25 16:32:40 字数 2277 浏览 1 评论 0原文

警告:这个问题中的 XML 是错误的,请先阅读答案,然后再混淆!


我已经把头撞在墙上有一段时间了。以下帖子阐明了该主题,但未能解决我的问题: ​​Android ListView 状态列表不显示默认项目背景ListView 项目背景通过自定义选择器

当我选择列表项时,正确的九块背景会完美显示,但我无法获得最初显示的默认九块背景。在我看来,我需要以某种方式设置默认项目背景,但我不知道该怎么做。

列表视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/shopListHeader"
    />
  <ListView
    android:id="@+id/shopList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="0px"
    android:divider="#FFFFFFFF"
    android:listSelector="@drawable/shop_list_selector"
    />
</LinearLayout>

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:visible="true">

    <!-- the list items are enabled and being pressed -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/shop_list_item_pressed" />

    <item
        android:state_selected="true"
        android:textColor="#FFFFFFFF" />
</selector>

背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:visible="true">

    <item
      android:state_selected="true"
      android:drawable="@android:color/transparent" />

    <item
        android:drawable="@drawable/shop_list_item" />
</selector>

如您所见,我已经简化了参考文献中的示例。

您可能还注意到背景选择器没有在任何地方被引用。我开始将它扔在随机的地方(如果应用程序编译的添加没有任何效果或导致强制关闭)

我还尝试在选择一个项目时阻止文本的颜色变为黑色和灰色,但没有按下(可以通过滚动列表来完成)。由于我的背景中心为黑色,因此选择时文本将部分不可见。据我所知,该添加(选择器中的最后一个项目节点)没有任何作用。

有谁对让这个极其耗时的功能发挥作用有什么想法吗?

WARNING: The XML in this question is wrong, read the answer before you confuse yourself!


I have been banging my head on the wall for a while now. The following posts have shed light on the subject, but failed to solve my issue: Android ListView State List not showing default item background and ListView item background via custom selector

The proper nine-patch background shows perfectly when I select the list item, but I can not get the default nine-patch background to show initially. It seems to me that I need to set the default item background somehow, but I can't figure out how to do so.

List View:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/shopListHeader"
    />
  <ListView
    android:id="@+id/shopList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="0px"
    android:divider="#FFFFFFFF"
    android:listSelector="@drawable/shop_list_selector"
    />
</LinearLayout>

Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:visible="true">

    <!-- the list items are enabled and being pressed -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/shop_list_item_pressed" />

    <item
        android:state_selected="true"
        android:textColor="#FFFFFFFF" />
</selector>

Background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:visible="true">

    <item
      android:state_selected="true"
      android:drawable="@android:color/transparent" />

    <item
        android:drawable="@drawable/shop_list_item" />
</selector>

As you can see, I have dumbed down the examples from the references.

You may also notice that the Background selector isn't being referenced anywhere. I started tossing it in random places (if the app compiled the addition either had no effect or cause a forced close)

I have also made an attempt to stop the color of the text from changing to black and grey when an item is selected but not pressed (can be done by scrolling through the list). Since my background will be black in the center, the text becomes partially invisible when selected. That addition (the last item node in the Selector) does nothing, as far as I can tell.

Does anyone have any thoughts on getting this ridiculously time consuming functionality working?

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

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

发布评论

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

评论(2

娇纵 2024-11-01 16:32:40

我本来打算删除这个线程,但我不能,所以我会看看我是否不能使用它作为不该做的事情的示例:)

首先,在 ListView XML 中: android:listSelector="@可绘制/shop_list_selector"
不要那样做!

我在这里尝试做的是设置列表项的背景,但 android:background 属性不起作用。您可能已经注意到,项目 XML 丢失了,那是因为它在我的脑海中丢失了! (我在解决这个“问题”的无数个小时中从未碰过它)因此,将 android:background="@drawable/shop_list_selector" 行放入项目的属性中,一切都很精彩。 (记住上面的 XML 是非常错误的,所以不要使用它!)

...好吧,除了它在现实生活中看起来不像我想象的那么好:(
回到绘图板!!!

I was gonna delete this thread, but I can't so I'll see if I can't use this as an example of what not to do :)

First, in the ListView XML: android:listSelector="@drawable/shop_list_selector"
Don't do that!

What I was trying to do here was set the background of the list items and the android:background property didn't work. You may have noticed that the item XML is missing, and that is because it was missing from my head! (I never touched it over the countless hours I was hammering away at this 'issue') So the line android:background="@drawable/shop_list_selector" goes in the item's properties and everything is groovy. (Remember the XML above is very wrong so don't use it!)

...Well except that it doesn't look as good in real life as it did in my head :(
Back to the drawing board!!!

演多会厌 2024-11-01 16:32:40

您尚未定义“正常”状态,请参阅此处

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue"
          android:state_pressed="true" />
    <item android:color="@color/red"
          android:state_selected="true" />
    <item android:color="@color/white" />
</selector>

的示例,白色是“正常”状态,位于 在这里您可以找到一些有关它的文档。

我希望这有帮助

You havent defined a "normal" state, see this example

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue"
          android:state_pressed="true" />
    <item android:color="@color/red"
          android:state_selected="true" />
    <item android:color="@color/white" />
</selector>

in here white is the "normal" state, in here you can find some documentation about it.

I hope this helps

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