安卓:创建一个切换按钮的图像和没有文字 [英] Android: Create a toggle button with image and no text

查看:144
本文介绍了安卓:创建一个切换按钮的图像和没有文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是有可能创造在Android的一个切换按钮,有一个形象,但没有文字? 理想情况下,应该是这样的:

我见过类似的帖子在哪里,答案是改变的背景,但我想preserve的全息光布局,只是换用图像中的文本。

我需要能够programaticallly改变图像源,

任何想法我怎么会做这个?

如果这个不能做,有没有办法可以让一个普通按钮开启和关闭?

解决方案
  1. 我可以用图像替换切换文本

    没有,我们不能,但我们可以通过overiding切换按钮的默认样式隐藏文本,但还是这样做不会给我们你想要,我们不能替换图像中的文字切换按钮。

  2. 我怎样才能让一个正常的切换按钮

    创建一个文件的 ic_toggle RES /绘制文件夹

     

     <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
        <项目
            机器人:state_checked =假
            机器人:可绘制=@可绘制/ ic_slide_switch_off/>
        <项目
            机器人:state_checked =真
            机器人:可绘制=@可绘制/ ic_slide_switch_on/>
    < /选择器>
     

    下面 @绘制/ ic_slide_switch_on &放大器; @绘制/ ic_slide_switch_off 是您创建的图像。

    然后创建在同一个文件夹中的另一个文件,将其命名为 ic_toggle_bg

     < XML版本=1.0编码=UTF-8&GT?;
    <层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ +安卓ID /背景
          机器人:可绘制=@机器人:彩色/透明/>
    <项目机器人:ID =@ +安卓ID /切换
          机器人:可绘制=@可绘制/ ic_toggle/>
    < /层列表>
     

    现在添加到您的自定义主题,(如果你没有一个建立在styles.xml文件中的 RES /价值/ 文件夹)

     <样式名称=Widget.Button.Toggle父=机器人:小工具>
       <项目名称=机器人:背景> @可绘制/ ic_toggle_bg< /项目>
       <项目名称=?安卓disabledAlpha>机器人:ATTR / disabledAlpha< /项目>
    < /风格>
    
    <样式名称=切换按钮父=@安卓Theme.Black>
       <项目名称=机器人:buttonStyleToggle> @风格/ Widget.Button.Toggle< /项目>
       <项目名称=机器人:纹元>< /项目>
       <项目名称=机器人:textOff>< /项目>
    < /风格>
     

    这将创建一个自定义切换按钮你。

  3. 如何使用它

    使用自定义样式和背景在您的视图。

     <切换按钮
            机器人:ID =@ + ID /切换按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =match_parent
            机器人:layout_gravity =右
            风格=@风格/切换按钮
            机器人:背景=@可绘制/ ic_toggle_bg
             />
     

is it possible to create a toggle button in Android that has an image but no text? 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.

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

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

    Then create another file in the same folder, name it 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)

    <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"
             />
    

这篇关于安卓:创建一个切换按钮的图像和没有文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆