Example of using Android custom shape

Source: Internet
Author: User

MainActivity is as follows: Copy codeThe Code is as follows: package cn. testshape;
Import android. OS. Bundle;
Import android. app. Activity;
/**
* Demo description:
* Use of custom shapes
*/
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
}

Main. xml is as follows:Copy codeThe Code is as follows: <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"
>
<Button
Android: layout_width = "250dip"
Android: layout_height = "50dip"
Android: text = "test the use of custom shapes"
Android: background = "@ drawable/background_selector"
Android: textColor = "@ drawable/textcolor_selector"
Android: layout_centerInParent = "true"
Android: gravity = "center"
/>
</RelativeLayout>

Background_selector.xml is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: drawable = "@ drawable/pressed_shape" android: state_pressed = "true"/>
<Item android: drawable = "@ drawable/default_shape"/>
</Selector>

Default_shape.xml is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Define the rectangle of a rectangle -->
<Shape xmlns: android = "http://schemas.android.com/apk/res/android"
Android: shape = "rectangle">
<! -- Define border color -->
<Solid android: color = "# d1d1d1"/>
<! -- Define the arc radians -->
<Corners
Android: bottomLeftRadius = "4dp"
Android: bottomRightRadius = "4dp"
Android: topLeftRadius = "4dp"
Android: topRightRadius = "4dp"
/>
</Shape>

Pressed_shape.xml is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Define the rectangle of a rectangle -->
<Shape xmlns: android = "http://schemas.android.com/apk/res/android"
Android: shape = "rectangle">
<! -- Define border color -->
<Solid android: color = "#7bb3f8"/>
<! -- Define the arc radians -->
<Corners
Android: bottomLeftRadius = "4dp"
Android: bottomRightRadius = "4dp"
Android: topLeftRadius = "4dp"
Android: topRightRadius = "4dp"
/>
</Shape>

Textcolor_selector.xml is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: color = "# ffffff" android: state_pressed = "true"/>
<Item android: color = "#000000" type = "codeph" text = "/codeph"/>
</Selector>

Related Article

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.