【问题标题】:Creating a selector创建选择器
【发布时间】:2017-11-13 03:45:32
【问题描述】:

我想在 Android Studio 中创建一个选择器。我在 res.layout 文件夹中创建了一个名为“button_hover.xml”的文件。


button_hover.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="wrap_content" android:layout_width="wrap_content">
    <item android:state_focused="true" android:drawable="@drawable/bg1.png"/>
    <item android:state_pressed="true" android:drawable="@drawable/bg2.png" />
    <item android:drawable="@drawable/bg3.png" />
</selector>

bg1.png, bg2.png, bg3.png 存在于我的drawable文件夹中,我可以使用Ctrl+Space来调用它们,但是当我运行应用程序时,控制台显示错误:

Error:(5, 58) No resource found that matches the given name (at 'drawable' with value '@drawable/bg1.png').
Error:(6, 58) No resource found that matches the given name (at 'drawable' with value '@drawable/bg2.png').
Error:(7, 29) No resource found that matches the given name (at 'drawable' with value '@drawable/bg3.png').

我该如何解决这个问题?

【问题讨论】:

  • 改用@drawable/bg1,丢掉.png
  • 从使用drawable的地方删除.png扩展名

标签: android android-layout android-drawable android-selector


【解决方案1】:

选择器 XML

<?xml version="1.0" encoding="utf-8"?>

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

    <item android:state_focused="true"
        android:drawable="@drawable/select_false" />

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

select_true XML

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

<solid
    android:color="#44ffffff"></solid>

<stroke android:color="#ffffff"
    android:width="1.2dp"/>

</shape>

select_false XML

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

<solid
    android:color="#00ffffff"></solid>

<stroke android:color="#ffffff"
    android:width="1.3dp"/>

</shape>

【讨论】:

    【解决方案2】:

    您能否检查一下它们是在 res/drawable 下创建的,它们似乎来自这些文件夹中的任何一个:

    /drawable-ldpi For low density screens.
    /drawable-mdpi For medium density screens.
    /drawable-hdpi For high resolution screens.
    /drawable-xhdpi For extra high resolution screens.
    

    如果您使用的是 android,然后从结构顶部选择项目,您将看到您的可绘制对象在哪个文件夹下,如果您不使用所有尺寸,它们应该在可绘制对象下。

    【讨论】:

      猜你喜欢
      • 2016-10-11
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2012-08-25
      相关资源
      最近更新 更多