繁体   English   中英

无法在Android自定义切换按钮中切换图像

[英]Images not Switching in Android Custom Toggle Button

在我的android应用程序中,我试图使一个简单的切换按钮在两个图像之间切换,实际上是一个标记为收藏夹星星 按钮的状态正在更改,但图像在ON和OFF状态下未切换。 默认情况下,连续显示OFF状态的图像。

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

** 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>

有人可以指导我我想念什么吗?

尝试像这样简化您的选择器:

<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设置为背景?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM