Android Custom Components

Source: Internet
Author: User

Official documents

/myselfcomponent/res/values/attrs.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "MyView" > &L T;attr name= "TextColor" format= "color"/> <attr name= "textSize" format= "Dimension"/> <attr name= "Text" format= "string" ></attr> </declare-styleable></resources>

Activity_main.xml

<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.myselfcomponent.MainActivity " xmlns:my=" http://schemas.android.com/apk/res/ Com.example.myselfcomponent ">    <com.example.myselfcomponent.MyView          android:id= "@+id/myview1"          android:layout_width= "Match_parent"         android:layout_height= "Wrap_content"         my: Textcolor= "#00ffff"         my:textsize= "20SP"          my:text= "Custom Components"                  /></RelativeLayout><!--The prefix of the  my:textcolor= "#00ffff" is related to the namespace (tools : context= "com.example.myselfcomponent.MainActivity"  xmlns:my= "http://schemas.android.com/apk/res/ Com.example.myselfcomponent ">", you can customize  -->

MyView

Package com.example.myselfcomponent;import android. R.integer;import android. r.style;import android.content.context;import android.content.res.typedarray;import  android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import  Android.graphics.paint.style;import android.graphics.rect;import android.util.attributeset;import  android.view.View;/** *  Custom Components  * */public class MyView extends  view{//Brush paint paint; String text;public myview (Context context)  {super (Context);p aint=new paint (); Paint.setcolor (Color.Black);p aint.settextsize ();// todo auto-generated constructor stub} Public myview (context context,attributeset attrs)  {// TODO Auto-generated  Constructor stubsuper (context, attrs);p aint=new paint (); Typedarray typedarray=context.obtainstyledattributes (Attrs,r.styleAble. MyView); Int color=typedarray.getcolor (R.STYLEABLE.MYVIEW_TEXTCOLOR,&NBSP;0XFFFFFF); float size= Typedarray.getdimension (r.styleable.myview_textsize, 20); Text=typedarray.getstring (R.styleable.MyView_ Text);p Aint.setcolor (color);p aint.settextsize (size);//Close Resource typedarray.recycle ();} /** *  is triggered when the component is initialized, rendering of components  * Canvas    canvas  * */@Overrideprotected  void ondraw (Canvas canvas)  {// TODO Auto-generated method  Stubsuper.ondraw (canvas);//Set brush style is solid paint.setstyle (Style.fill);//Draw Square canvas.drawrect (New rect ( 10,10,90,90),  paint);p Aint.setcolor (Color.Blue); Canvas.drawtext (Text, 10, 110, paint);}}

Mainactivity

Package com.example.myselfcomponent;import android.app.activity;import android.os.bundle;import  android.view.menu;import android.view.menuitem;public class mainactivity extends  Activity {  @Overrideprotected  void oncreate (bundle savedinstancestate)  { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic  boolean oncreateoptionsmenu (Menu menu)  {// inflate the menu ; this adds items to the action bar if it is  Present.getmenuinflater (). Inflate (r.menu.main, menu); return true;} @Overridepublic  boolean onoptionsitemselected (Menuitem item)  {// handle action  bar item clicks here. The action bar will// automatically  Handle clicks on the home/up button, so long// as you specify a parent activity in AndroidManifest.xml.int id =  Item.getitemid ();if  (id == r.id.action_settings)  {return true;} return super.onoptionsitemselected (item);}}

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/8B/9C/wKiom1hSVObiPBTvAABjsOc-PRs418.png-wh_500x0-wm_3 -wmp_4-s_474046675.png "title=" z1@ (EU ' s_vi2id8z0i3klhy.png "alt=" Wkiom1hsvobipbtvaabjsoc-prs418.png-wh_50 "/>

This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1883078

Android Custom Components

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.