在我的Android应用程序中,我试图在两个图像之间切换一个简单的切换按钮,实际上是明星以标记为收藏夹。按钮的状态正在改变,但图像没有切换为ON和OFF状态。默认情况下,连续显示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>
有人可以指导我错过了什么吗?
答案 0 :(得分:0)
尝试简化您的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设置为背景吗?