【问题标题】:Images not Switching in Android Custom Toggle Button图像未在 Android 自定义切换按钮中切换
【发布时间】:2014-06-03 15:37:15
【问题描述】:

在我的 android 应用程序中,我试图在两个图像之间进行简单的切换按钮切换,实际上是一个 star 来标记为 favorites。按钮的状态正在更改,但图像不会在 ON 和 OFF 状态下切换。关闭状态的图像默认连续显示。

我尝试了以下教程: http://www.coderzheaven.com/2012/05/20/create-custom-toggle-button-android/ http://www.technotalkative.com/android-custom-toggle-button-example/

** togbtn_fav_custom.xml **

<item android:drawable="@drawable/ic_action_important" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_action_important" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/ic_action_not_important" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_action_not_important" android:state_checked="false" android:state_focused="false"/>

layout.xml

<LinearLayout 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:orientation="vertical" 

    >
  <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:id="@+id/ll_back_single_dua_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center_vertical" >

        <Button
            android:id="@+id/btn_play_pause_dua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/play_btn_custom" />


        <TextView
            android:id="@+id/tv_back_single_dua_footer_counter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:singleLine="true"
            android:text="@string/sample_counter"

            android:textSize="23sp" />

        <ToggleButton
            android:id="@+id/tog_btn_favorite_dua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        android:background="@drawable/togbtn_fav_custom"
        android:textOff=""
        android:textOn=""
         />
    </LinearLayout>

    </LinearLayout>

谁能指导我我错过了什么?

【问题讨论】:

  • 你的xml文件名是selector.xml还是togbtn_fav_custom.xml?
  • togbtn_fav_custom.xml
  • 正确检查您的文件。

标签: android togglebutton


【解决方案1】:

尝试像这样简化您的 selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ic_action_not_important" android:state_checked="false"/>
  <item android:drawable="@drawable/ic_action_important" android:state_checked="true"/>
</selector>

正如阿基里斯所说,检查线

android:background="@drawable/togbtn_fav_custom"

可能需要将 selector.xml 设置为背景?

【讨论】:

  • 我在另一个活动中尝试了相同的代码,它工作正常。当我在 Fragment 中使用上述代码时会出现问题。你能解释一下,为什么?
猜你喜欢
  • 2018-08-05
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2016-08-27
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多