简体   繁体   English

切换按钮充当图像按钮

[英]Toggle Button acting as an Image Button

I need a toggle button to have an image and a background color. 我需要一个切换按钮来具有图像和背景色。 Currently, my toggle button only has an image and no background color. 目前,我的切换按钮只有一张图片,没有背景色。 Here is the code for it 这是它的代码

<ToggleButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:onClick="onToggleClicked"
android:textSize="0sp"
android:checked="false"
android:background="@drawable/soundfocuslone1"/>


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<bitmap android:src="@drawable/soundonlone1"
android:gravity="center"/>
</item>
<item android:state_pressed="true">
<bitmap android:src="@drawable/soundfocuslone1"
android:gravity="center" />
</item>
<item android:state_checked="false">
<bitmap android:src="@drawable/soundofflone1"/>
</item>
</selector>

Is it possible to have two backgrounds on the toggle button? 切换按钮上可能有两个背景吗? I want a background color as well as an image on it. 我想要背景色以及图像。 If not, is there a simple way to have an image button behave like a toggle button? 如果不是,是否有一种简单的方法来使图像按钮的行为类似于切换按钮? Having an image button acting as a toggle button would probably be easier as the image is not scaled on the toggle button. 将图像按钮用作切换按钮可能会更容易,因为图像不会在切换按钮上缩放。 Thanks, and I would appreciate all replies. 谢谢,感谢您的答复。

if you dont want the image to be placed in the center, use this for giving background. 如果您不希望将图像放置在中间,请使用此背景。

1 . 1 put this in your toggle button - android:background="@drawable/bw_toggle_on_selector" 将其放在您的切换按钮中-android:background =“ @ drawable / bw_toggle_on_selector”

2 . 2 create an XML in drawable folder with the name "bw_toggle_on_selector" and paste the below code in it- 在名称为“ bw_toggle_on_selector”的可绘制文件夹中创建XML,并将以下代码粘贴到其中-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#7995A8"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
 android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
 </shape>  

3 .use android:drawableLeft for setting background image. 3。使用android:drawableLeft设置背景图像。

and if you want to place the image in center, then use this code (it covers everything incl. background image). 并且如果您想将图像放置在中间,请使用此代码(它涵盖了包括背景图像在内的所有内容)。

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item 
    android:state_pressed="true">
    <layer-list>
        <item>
            <bitmap 
                android:gravity="center" 
                android:src="@drawable/ic_stop" 
                 />
        </item>
        <item>
            <shape>
                 <gradient 
                     android:angle="270" 
                     android:endColor="#a0e0b071" 
                     android:startColor="#a0a67637" />
                 <stroke 
                     android:width="1dp" 
                     android:color="#5c3708" />
                 <corners 
                     android:radius="5dp" />
                 <padding 
                     android:bottom="10dp" 
                     android:left="10dp" 
                     android:right="10dp" 
                     android:top="10dp" />
            </shape>
        </item>
    </layer-list>
</item>

<item 
    android:state_enabled="true">
    <layer-list>
        <item>
            <bitmap 
                android:gravity="center" 
                android:src="@drawable/ic_back" 
                 />
        </item>
        <item>
            <shape 
                android:shape="rectangle">
                <gradient 
                    android:angle="270" 
                    android:endColor="#a0a67637" 
                    android:startColor="#a0e0b071" />
                <stroke 
                    android:width="1dp" 
                    android:color="#5c3708" />
                <corners 
                    android:radius="5dp" />
                <padding 
                    android:bottom="10dp" 
                    android:left="10dp" 
                    android:right="10dp" 
                    android:top="10dp" />
            </shape>
        </item>
    </layer-list>
</item>


</selector>

** Some Tips ** ** 一些技巧 **

*Changing Button text onClick *更改按钮文字onClick

1 . 1 create a XML file in Drawable folder with the name "text_color.xml" 在Drawable文件夹中创建一个名称为“ text_color.xml”的XML文件

paste the below code in it- 将以下代码粘贴到其中-

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#000000" />
<item
android:state_pressed="false"
android:color="#01DFD7" />
 </selector>

2 . 2 put this in your button to define the XML android:textColor="@drawable/text_color" 将其放在您的按钮中以定义XML android:textColor =“ @ drawable / text_color”

*Faster way of Resizing my images? *更快地调整图像大小?

use this tool or Android asset studio (google it) 使用此工具或Android Asset Studio(对它进行Google搜索)

*How do i make a Custom Background for my Buttons? *如何为按钮创建自定义背景?

try this online tool :) 试试这个在线工具 :)

First of all you write inside your xml file: 首先,您在xml文件中编写以下代码:

   <ToggleButton
                        android:id="@+id/tgTextReader"
                        android:layout_width="60dp"
                        android:layout_height="36dp"
                        android:background="@drawable/bw_toggle_on_selector"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:textOff=""
                        android:textOn="" />

then create folder inside res/ 然后在res /中创建文件夹
drawable folder write xml file name is bw_toggle_on_selector.xml drawable文件夹写的xml文件名是bw_toggle_on_selector.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/a"
          android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/b"
        android:state_checked="false"/>

 </selector>

this file keep in drawable foloder. 此文件保存在可绘制的文件中。 and also create a new folder inside res / 并在res /中创建一个新文件夹
drawable-nodpi where two image keep there. drawable-nodpi,其中有两个图像。 such as image a, image b. 例如图像a,图像b。 hops work successfully. 啤酒花工作成功。 Thanks 谢谢

Actually ToggleButton has "android:button" attribute. 实际上,ToggleButton具有“ android:button”属性。 So u can use "android:button" attribute for selector, and "android:background" for giving the background color to the ToggleButton.But using toggle button we can get the gravity issue so you can use check box instead of toggle button. 因此,您可以使用“ android:button”属性作为选择器,并使用“ android:background”属性为ToggleButton提供背景颜色。但是使用切换按钮,我们可以获得重力问题,因此可以使用复选框而不是切换按钮。

<CheckBox
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:onClick="onToggleClicked"
android:textSize="0sp"
android:checked="false"
android:padding="5dp"
android:button="@drawable/soundfocuslone1"
android:background="ur_background_color_code or drawable"/>

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

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