Android-styled Toggle Buttons

Source: Internet
Author: User

Android to the default UI is still somewhat more aesthetically pleasing than iOS to the default UI, and we want to be able to beautify the UI and replace the system's default UI style by using our custom to UI by default when using these UIs. This article takes ToggleButton as an example, describes how to use, is.


<?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/btn_toggle"/></layer-list>

Here, we use layer-list to divide the ToggleButton into 2 layers, with the background (set to transparent) and the top layer as the selector

Next, set the ToggleButton on and off selector:

Establish/res/drawable/btn_toggle.xml

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

Use the following two images, or find http://bit.ly/pn5dmA using the link below.


We have already finished making the ToggleButton effect.

2. Set Style & Theme

ToggleButton to have finished, next, we have to set this effect into a unified style, otherwise we have to use the time must be shown to specify this style, here, that is: in the declaration of ToggleButton, each time you have to specify Android: background= "@drawable/btn_toggle_bg".

We want to achieve the effect that when we use Tooglebutton each time, the default is to use the style we set. Before this, it is best to read the following Android bring to the documentation: basics of creating Android themes, have a certain understanding of style and theme.

Next, build the/res/drawable/themes.xml file

<style name="Widget.Button.Toggle"Parent="Android:widget"> <item name="Android:background"> @drawable/btn_toggle_bg</item> <item name="Android:texton"> @null </item> <item name="Android:textoff"> @null </item> <item name="android:clickable">true</item> <item name="Android:disabledalpha">?android:attr/disabledAlpha</item></style>

Here, to set the properties of the Texton and textoff to NULL, otherwise, the system will display the text on the button, which can be determined according to the usage.

Then, set the style to the theme

<style name="yourthemename" parent="@android: Theme.black" >    <!--tell Android when creating ToggleButton using custom Style--    <item name="android: Buttonstyletoggle"> @style/widget.button.toggle</item>    <!--Here you can add more options ...-- ></style>

After that, just set the theme in the <application> tab of Androidmanifest.xml (android:theme= "@style/yourthemename")

3. Precautions

    • < important > In order to provide a more general-purpose effect, the image should be set to a. 9.png format so that the image can be expanded
    • Many people have questions about how to know these controls or themes to attributes? These are found in the Android to the source code, the specific location in the \base\core\res\res\values\
    • Project source code Download: http://download.csdn.net/source/347022

Android-styled Toggle Buttons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.