Android custom control -- (5) Click the color change button. android Control

Source: Internet
Author: User

Android custom control -- (5) Click the color change button. android Control

-------------------------------- Click the color change button (attribute explanation is available in the source code )-----------------------------------------------------

 

 

I. shape style: (create in drawable -- new --> Drawable resource file and add Item in the parent tag selector)

<? Xml version = "1.0" encoding = "UTF-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#73c4f3" />
<stroke android:width="2dp" android:color="#ffffff" />
<corners android:radius="20dp" />
<padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
<gradient android:angle="270" android:endColor="#8accf2" android:startColor="#8accf2" android:type="sweep" />
</shape>
</item>

<item>
<shape>
<solid android:color="#3fb3f6" />
<stroke android:width="2dp" android:color="#ffffff" />
<corners android:radius="20dp" />
<padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
</shape>
</item>
</selector>

Ii. style:
<style name="rectangle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15sp</item>
<item name="android:textColor">#ffffff</item>
<item name="android:background">@drawable/buttonclickstyle</item>
</style>
 

3. The Button control calls the style:


<? Xml version = "1.0" encoding = "UTF-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ly.blogtest.MainActivity">

<Button
android:id="@+id/button"
style="@style/rectangle"
android:text="@string/btn1" />
</RelativeLayout>

 

---------------------------------- Click the color change button -----------------------------------------------------

 

---------------------------------- Item attribute tag ------------------------------

  • Android: whether to press state_pressed, such as a button to touch or click.
  • Android: state_focused indicates whether to obtain the focus. For example, you have selected a text box.
  • Android: state_hovered: whether to hover the cursor. It is usually the same as the focused state. It is a new feature of 4.0.
  • Android: state_selected is selected. It is not exactly the same as the focus state. When a list view is selected, its child components may be selected through the direction keys.
  • Android: whether the state_checkable component can be checked. For example, RadioButton can be checked.
  • Android: state_checked has been checked. For example, a RadioButton can be checked.
  • Android: state_enabled can accept touch or click events
  • Android: state_activated activated
  • Android: state_window_focused

Whether the application is in the foreground. When a notification bar is pulled down or a dialog box pops up, the application is not in the foreground.

NOTE: If there are multiple items, the program will automatically match from top to bottom, and the first matching will be applied. (Not through the best match)

If an item does not have any state description, it can be matched by any State.

---------------------------------- Item attribute tag ------------------------------

Combine button shape and color attributesViewHttp://www.cnblogs.com/LOVEJIEYING/p/6016319.html

 

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.