我需要使用两张图片制作一个开关按钮,而不是使用ON/OFF状态。
在关闭状态下,我设置了一个背景图片。但当我使用背景图片时,无法去除OFF文本。
并且我无法通过点击开关按钮来设置另一张ON状态的图片。:( 我是Android方面的新手。希望你们能帮助我解决这个问题。
我需要使用两张图片制作一个开关按钮,而不是使用ON/OFF状态。
在关闭状态下,我设置了一个背景图片。但当我使用背景图片时,无法去除OFF文本。
并且我无法通过点击开关按钮来设置另一张ON状态的图片。:( 我是Android方面的新手。希望你们能帮助我解决这个问题。
请这样做:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/check" <!--check.xml-->
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
在drawable文件夹中创建check.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
android:background="@null"
和 android:drawableRight="@drawable/check"
。通常我发现切换按钮是右对齐的。如果您需要左对齐,请使用 android:drawableLeft
。 - PatrickAkashG的解决方案对我无效。当我将check.xml设置为背景时,它只是在垂直方向上被拉伸。要解决这个问题,您应该将check.xml设置为“android:button”属性:
AkashG的解决方案对我无效。当我将check.xml设置为背景时,它只是在垂直方向上被拉伸。要解决这个问题,您应该将check.xml设置为“android:button”属性:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check" //check.xml
android:background="@null"/>
check.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
android:background="@android:color/transparent"
。 - Pavelholder.imgitem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!onclick){
mSparseBooleanArray.put((Integer) view.getTag(), true);
holder.imgoverlay.setImageResource(R.drawable.ipad_768x1024_editmode_delete_overlay_com);
onclick=true;}
else if(onclick)
{
mSparseBooleanArray.put((Integer) view.getTag(), false);
holder.imgoverlay.setImageResource(R.drawable.ipad_768x1024_editmode_selection_com);
onclick=false;
}
}
});
1- https://dev59.com/5HI_5IYBdhLWcg3wFu7L
2- https://dev59.com/gm025IYBdhLWcg3wST6Q - Nermeen