Android custom Controls (iii) custom properties

Source: Internet
Author: User

The book is connected to the

Create attributes in XML, and then use Typedarray to get the attributes in Java code, and then use attributes to do something. Example: Get the color in XML and assign it to paint.

1. Create a new attrs.xml under res/values/

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <declare-styleable name="CustomView2">
  4. <attr name="TextColor" format="color" />
  5. <attr name="textSize" format="Dimension" />
  6. </declare-styleable>
  7. </Resources>
  8. <!--name= "CustomView1" Control name gets Typedarray
  9. <!--name= "TextColor" corresponds to Test:textcolor--
  10. <!--format= "color" corresponding to the construction method A.getcolor (R.styleable.customview2_textcolor, 0xFFFFFFFF); -

Format details can be referenced http://blog.csdn.net/ethan_xue/article/details/7315064
2. The main view of the constructor function

[Java]View Plaincopy
  1. Public class CustomView2 extends View {
  2. private Paint MPaint2;
  3. private String MText = "DrawText";
  4. Public CustomView2 (context context, AttributeSet attrs) {
  5. Super (context, attrs);
  6. MPaint2 = new Paint ();
  7. //Typedarray is the array,1 that holds the resource. This array is obtained through the context, Attrs is the constructor that is passed in, corresponding to Attrs.xml
  8. TypedArray a = Context.obtainstyledattributes (Attrs, R.STYLEABLE.CUSTOMVIEW2);
  9. //Get attributes defined in XML, formatted as name _ property name followed by default value
  10. int textcolor = A.getcolor (R.styleable.customview2_textcolor, 0xFFFFFFFF);
  11. float textSize = a.getdimension (r.styleable.customview2_textsize, 35);
  12. Mpaint2.setcolor (TextColor);
  13. Mpaint2.settextsize (textSize);
  14. //In order to maintain consistency with this attribute later, return a binding resource to the end of the signal to the resource
  15. A.recycle ();
  16. }
  17. @Override
  18. protected void OnDraw (canvas canvas) {
  19. Super.ondraw (canvas);
  20. Mpaint2.setstyle (Style.fill);
  21. Canvas.drawtext (MText, Ten, mPaint2);
  22. }
  23. }

3. Layout

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <!--xmlns:test= "Http://schemas.android.com/apk/res/ethan.customview1" package name--
  3. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:test="Http://schemas.android.com/apk/res/ethan.customview1"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. >
  9. <ethan.customview1.CustomView2
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. test:textcolor="#f00"
  13. test:textsize="20sp"
  14. />
  15. </linearlayout>

4.

http://download.csdn.net/detail/ethan_xue/4108832

Android custom Controls (iii) custom properties

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.