Android - 从形状 xml 引用的 ColorStateList xml 的问题

发布于 2024-11-28 21:17:58 字数 1793 浏览 1 评论 0原文

我遇到了一个问题,其中一个可绘制 xml 文件中的颜色选择器似乎不被接受。我有一个布局,包括:

<LinearLayout 
    android:layout_height="wrap_content"
    android:id="@+id/sortLayout"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:background="@color/listSortBarBackground" android:orientation="vertical">
    <ToggleButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:checked="true"
        android:background="@drawable/filter_button_left"/>
    <ToggleButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:checked="false"
        android:background="@drawable/filter_button_left"/>
</LinearLayout>

drawable\filter_button_left.xml 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="@color/filter_button_color" />
  <corners
    android:bottomLeftRadius="0dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="0dp"
    android:bottomRightRadius="5dp"/>
</shape>

和 color\filter_button_color.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:color="@color/myred"
    android:state_checked="true"/>
  <item
    android:color="@color/myblue"
    android:state_checked="false"/>
</selector>

(myred 和 myblue 在 color.xml 中定义)

按钮被渲染,我可以告诉他们正在获得正确的检查状态,因为第一个显示文本“ON”,第二个显示“OFF”,两个按钮都将形状作为背景,但在这两种情况下,形状的颜色都是红色。我尝试了翻转 filter_button_color.xml 选择器中的项目,似乎无论实际状态或选择器项目中的状态如何,始终使用顶部颜色。

谁能明白为什么这不起作用?

谢谢! 斯科特

I'm running into a problem where the color selector in one of my drawable xml files doesn't seem to be honored. I have a layout including:

<LinearLayout 
    android:layout_height="wrap_content"
    android:id="@+id/sortLayout"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:background="@color/listSortBarBackground" android:orientation="vertical">
    <ToggleButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:checked="true"
        android:background="@drawable/filter_button_left"/>
    <ToggleButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:checked="false"
        android:background="@drawable/filter_button_left"/>
</LinearLayout>

drawable\filter_button_left.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="@color/filter_button_color" />
  <corners
    android:bottomLeftRadius="0dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="0dp"
    android:bottomRightRadius="5dp"/>
</shape>

and color\filter_button_color.xml is:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:color="@color/myred"
    android:state_checked="true"/>
  <item
    android:color="@color/myblue"
    android:state_checked="false"/>
</selector>

(myred and myblue are defined in colors.xml)

The buttons are rendered and I can tell they are getting the proper checked state since the first one displays with text "ON" and the second one "OFF", both buttons get the shape as their background, but in both cases the shape's color is myred. I played around with flipping the items in the filter_button_color.xml selector, and it seems that regardless of the actual state or the states in the selector items, the top color is always used.

Can anyone see why this shouldn't work?

Thanks!
Scott

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

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

发布评论

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

评论(1

梦里°也失望 2024-12-05 21:17:58

我认为你的问题是 xml 绘图的方式错误。
您需要首先从 ToggleButton 布局引用您的选择器,并在选择器布局内有两个可绘制对象。
例如切换按钮->选择器(有两种状态)->形状。
这应该可行(并减少一个 xml 文件)。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/myred"/>
            <corners 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="0dp" 
                android:bottomRightRadius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <solid android:color="@color/myblue"/>
            <corners 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="0dp" 
                android:bottomRightRadius="5dp"/>
        </shape>
    </item>
</selector>

I think your problem is that you have the xml drawables the wrong way around.
You need to refer to your selector FIRST from the ToggleButton layout, and inside the selector layout, have the two drawables.
Eg ToggleButton -> Selector (With two states) -> Shapes.
This should work (and make one less xml file).

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/myred"/>
            <corners 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="0dp" 
                android:bottomRightRadius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <solid android:color="@color/myblue"/>
            <corners 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="0dp" 
                android:bottomRightRadius="5dp"/>
        </shape>
    </item>
</selector>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文