Android rewrite view and custom attribute instance analysis _android

Source: Internet
Author: User
Tags custom name

The example in this article analyzes the way that Android overrides view and customizes attributes. Share to everyone for your reference, specific as follows:

This is achieved by customizing the properties shown in the following illustration:

First step: Create a new file in the Res\values directory Attrs.xml

Declaring some custom properties

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
  <declare-styleable name= " Customviewstyle ">
    <attr name=" CustomText format= "string"/>
    <attr "Name=" format= "Color"/>
    <attr name= "customtextsize" format= "Dimension"/>
  </declare-styleable>
</resources>

Step Two: Create a new layout file in the layout directory Activity_main.xml

Pay special attention to adding this declaration to the outer control:

Format: xmlns: (your custom name) = "http://schemas.android.com/apk/(name of package you apply)"

Xmlns:xr= "Http://schemas.android.com/apk/res/com.rong.test"

Or

Xmlns:xr= "Http://schemas.android.com/apk/res-auto"

Recommended use of the second

Add these custom properties to the layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
  xmlns:xr=" http://schemas.android.com/apk/res/com.rong.test "
  android:layout_width=" Match_ Parent "
  android:layout_height=" match_parent "
  android:background=" @android: Color/black "
  android:o rientation= "vertical" >
  <com.rong.activity.customview
    android:layout_width= "300DP"
    android: layout_height= "300DP"
    android:layout_centerinparent= "true"
    android:background= "#ff0000"
    XR: customtext= "Custom Control"
    xr:customtextcolor= "#000000"
    xr:customtextsize= "40sp"/>
</ Relativelayout>

Part III inheritance View rewrite

Package com.rong.activity;
Import COM.RONG.TEST.R;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
Import Android.util.AttributeSet;
Import Android.view.View; /** * Custom Control * * @author Xu Rong * */public class CustomView extends View {/** * custom brushes/Private Paint Mpaint
  ;
  /** * Text range * * Private Rect mbounds;
  /** * Custom text/private String customtext;
  /** * Custom Size * * Private int customtextsize;
  /** * Custom color/private int customtextcolor;
    Public CustomView (context, AttributeSet attrs) {Super (context, attrs);
    TypedArray TypedArray = context.obtainstyledattributes (Attrs, R.styleable.customviewstyle);
    Gets the custom text CustomText = typedarray.getstring (R.styleable.customviewstyle_customtext); Get custom Text Size customtextsize = typedarray.getdimensionpixelsize (r.styleable.custoMviewstyle_customtextsize, 28);
    or custom text color Customtextcolor = Typedarray.getcolor (R.styleable.customviewstyle_customtextcolor, Color.WHITE);
    To recycle this TypedArray object Typedarray.recycle ();
  Initview ();
    public void Initview () {//init brush mpaint = new Paint ();
    Mpaint.setantialias (TRUE);
    Mpaint.setstyle (Paint.Style.FILL);
    Mpaint.setcolor (Customtextcolor);
    Mpaint.settextsize (customtextsize);
  Generate text Area mbounds = new Rect ();
    } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
    Get text display area Mbounds mpaint.gettextbounds (customtext, 0, Customtext.length (), mbounds);
    Center text wide = width of the control/2-width of text/2 float helfwidth = getwidth ()/2-mbounds.width ()/2;
    Make text High center = width of Control/2 + text width/2 float helfheight = getheight ()/2+mbounds.height ()/2;
  Draw Text Canvas.drawtext (CustomText, Helfwidth, Helfheight, Mpaint);

 }
}

Run!

More interested readers of Android-related content can view the site's topics: Introduction to Android Development and advanced tutorials, the summary of Android basic components usage, the overview of Android View View techniques, the Android Layout layout skills Summary, and A summary of the usage of Android controls

I hope this article will help you with the Android program.

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.