Android and android Official Website

Source: Internet
Author: User

Android and android Official Website

When we customize the View, when assigning some length and width to the View, it is generally carried out in the layout file ., For example, for android: layout_height = "wrap_content", you can also define attributes by yourself. In this way, you can use myapp: myTextSize = "20sp.

Values/attrs. xml

First, create a variable and create a values/attrs. xml file, or the file name is arbitrary, but in the values directory:

<?xml version="1.0" encoding="utf-8"?>   <resources>       <declare-styleable name="MyView">           <attr name="textSize" format="dimension" />       </declare-styleable>   </resources>

Here, resource is a tag, and several declare-styleable can be defined in it. in <declare-styleable name = "MyView">, name defines the variable name, you can customize multiple attributes. For <attr name = "textSize" format = "dimension"/>, the attribute name is "textSize ", format specifies that the attribute type is dimension, which can only represent the font size.

  • You can also specify other types such:
  • Reference indicates reference. Refer to a resource ID.
  • String indicates a string.
  • Color indicates the color value.
  • Dimension indicates the size value.
  • Boolean indicates a boolean value.
  • Integer indicates the integer value.
  • Float indicates the floating point value.
  • Fraction indicates percentage
  • Enum indicates the enumerated value.
  • Flag bitwise operation
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      tools:context=".MainActivity" >      <us.yydcdut.MyView          android:layout_height="wrap_content"          android:layout_width="wrap_content"          myapp:textSize="20sp"          myapp:myColor="#324243"/>    </LinearLayout>

You can see the xmlns: myapp = "http://schemas.android.com/apk/res/com.eyu.attrtextdemo", and in the Custom View myapp: textSize = "20sp", myapp: myColor = "#324243"

ObtainStyledAttributes

Context obtains a TypeArray by calling the obtainStyledAttributes method, and then sets the attribute by the TypeArray method.

There are three obtainStyledAttributes methods. The most common method is to have an obtainStyledAttributes (int [] attrs) parameter, which is obtained directly from styleable.

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);

The recycle () method must be called after the call ends. Otherwise, this setting will affect the next use.

public class MyView extends View{      public Paint paint;        public MyView(Context context, AttributeSet attrs) {          super(context, attrs);          paint = new Paint();                    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);              int textColor = a.getColor(R.styleable.MyView_myColor, 003344);          float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);          paint.setTextSize(textSize);          paint.setColor(textColor);          a.recycle();      }        public MyView(Context context) {          super(context);      }            @Override     protected void onDraw(Canvas canvas) {          super.onDraw(canvas);             paint.setStyle(Style.FILL);          canvas.drawText("aaaaaaa", 10, 50, paint);      }        }
I am the dividing line of tiantiao

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.