简体   繁体   English

Android:创建一个带有图片且没有文字的切换按钮

[英]Android: Create a toggle button with image and no text

is it possible to create a toggle button in Android that has an image but no text? 是否可以在Android中创建一个具有图片但没有文字的切换按钮? Ideally it would look like this: 理想的情况是这样的:

切换图像按钮

Ive seen similar posts where the answer was to change the background but i want to preserve the Holo Light layout and just swap the text with an image. 我见过类似的帖子,答案是改变背景,但我想保留Holo Light布局,只是将文本与图像交换。

I need to be able to programaticallly change the image source, 我需要能够以编程方式更改图像源,

Any ideas how i would make this? 任何想法,我将如何做到这一点?

If this cant be done, is there a way i can make a normal button toggle on and off? 如果无法完成此操作,是否有办法使正常按钮打开和关闭?

  1. Can I replace the toggle text with an image 我可以用图像替换切换文本吗

    No, we can not, although we can hide the text by overiding the default style of the toggle button, but still that won't give us a toggle button you want as we can't replace the text with an image. 不,我们不能,尽管我们可以通过覆盖切换按钮的默认样式来隐藏文本,但是由于我们无法用图像替换文本,因此仍然无法提供所需的切换按钮。

  2. How can I make a normal toggle button 如何制作普通的切换按钮

    Create a file ic_toggle in your res/drawable folder res/drawable文件夹中创建文件ic_toggle

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

    Here @drawable/ic_slide_switch_on & @drawable/ic_slide_switch_off are images you create. @drawable/ic_slide_switch_on@drawable/ic_slide_switch_off是您创建的图像。

    Then create another file in the same folder, name it ic_toggle_bg 然后在同一文件夹中创建另一个文件,将其命名为ic_toggle_bg

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+android:id/background" android:drawable="@android:color/transparent" /> <item android:id="@+android:id/toggle" android:drawable="@drawable/ic_toggle" /> </layer-list> 

    Now add to your custom theme, (if you do not have one create a styles.xml file in your res/values/ folder) 现在添加到您的自定义主题,(如果您没有,请在res/values/文件夹中创建一个styles.xml文件)

     <style name="Widget.Button.Toggle" parent="android:Widget"> <item name="android:background">@drawable/ic_toggle_bg</item> <item name="android:disabledAlpha">?android:attr/disabledAlpha</item> </style> <style name="toggleButton" parent="@android:Theme.Black"> <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item> <item name="android:textOn"></item> <item name="android:textOff"></item> </style> 

    This creates a custom toggle button for you. 这会为您创建一个自定义切换按钮。

  3. How to use it 如何使用它

    Use the custom style and background in your view. 在视图中使用自定义样式和背景。

      <ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="right" style="@style/toggleButton" android:background="@drawable/ic_toggle_bg"/> 

ToggleButton inherits from TextView so you can set drawables to be displayed at the 4 borders of the text. ToggleButton继承自TextView因此您可以将可绘制对象设置为在文本的4个边框处显示。 You can use that to display the icon you want on top of the text and hide the actual text 您可以使用它在文本上方显示所需的图标并隐藏实际文本

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@android:drawable/ic_menu_info_details"
    android:gravity="center"
    android:textOff=""
    android:textOn=""
    android:textSize="0dp" />

The result compared to regular ToggleButton looks like 与常规ToggleButton相比,结果看起来像

在此处输入图片说明

The seconds option is to use an ImageSpan to actually replace the text with an image. 秒选项是使用ImageSpan实际用图像替换文本。 Looks slightly better since the icon is at the correct position but can't be done with layout xml directly. 由于图标位于正确的位置,因此看起来稍好一些,但是不能直接使用xml布局完成。

You create a plain ToggleButton 您创建一个普通的ToggleButton

<ToggleButton
    android:id="@+id/toggleButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false" />

Then set the "text" programmatially 然后以编程方式设置“文本”

ToggleButton button = (ToggleButton) findViewById(R.id.toggleButton3);
ImageSpan imageSpan = new ImageSpan(this, android.R.drawable.ic_menu_info_details);
SpannableString content = new SpannableString("X");
content.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(content);
button.setTextOn(content);
button.setTextOff(content);

The result here in the middle - icon is placed slightly lower since it takes the place of the text. 此处的结果位于中间-图标,因为它代替了文本,因此位置稍低。

在此处输入图片说明

create toggle_selector.xml in res/drawable 在res / drawable中创建toggle_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/toggle_on" android:state_checked="true"/>
  <item android:drawable="@drawable/toggle_off" android:state_checked="false"/>
</selector>

apply the selector to your toggle button 将选择器应用于切换按钮

<ToggleButton
            android:id="@+id/chkState"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_selector"
            android:textOff=""
            android:textOn=""/>

Note: for removing the text i used following in above code 注意:删除上面代码中我使用的文本

textOff=""
textOn=""

I know this is a little late, however for anyone interested, I've created a custom component that is basically a toggle image button, the drawable can have states as well as the background 我知道这有点晚了,但是对于任何有兴趣的人,我创建了一个自定义组件,该组件基本上是一个切换图像按钮,drawable可以具有状态以及背景

https://gist.github.com/akshaydashrath/9662072 https://gist.github.com/akshaydashrath/9662072

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

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