Kotlin Getting Started (24) How to customize a view

Source: Internet
Author: User
Tags getcolor

Android offers a variety of views and controls that are already able to meet most of the business needs, but the plan is not up to the changes, and there are always unexpected situations that require special handling. For example, Pagertabstrip cannot specify text size and text color in the layout file, and can only be set in code by Settextsize and SetTextColor methods. This is inconvenient, if it can be like TextView directly in the layout to specify the text size and color is good; to allow Pagertabstrip to support this feature, you have to use a custom view, and the first way to customize the view is to customize the properties.
Still pagertabstrip for example in the page title bar, now adds two custom properties to it, namely the text color TextColor, and the text size textsize. The following are the custom steps for Java encoding:
1. In res\ Attrs.xml is created under the values directory, the contents of the file are as follows, where the Declare-styleable Name property value represents the name of the new view, and two attr nodes indicate that the two new properties are TextColor and TEXTSIZE, respectively:

<resources>    <declare-styleable name= "Custompagertab" >        <attr name= "textcolor" format= "color "/>        <attr name=" textSize "format=" Dimension "/>    </declare-styleable></resources>

2. Create the Custompagertab.java in the widget directory of the module, filling in the code for the following custom views:

public class Custompagertab extends Pagertabstrip {private int textcolor = Color.    BLACK;    private int textSize = 15;    Public Custompagertab (Context context) {super (context);        } public Custompagertab (context context, AttributeSet Attrs) {Super (context, attrs); The constructor reads the Custompagertab custom property from Attrs.xml if (attrs! = null) {TypedArray attrarray=getcontext (). obtainstyl            Edattributes (Attrs, R.styleable.custompagertab);            TextColor = Attrarray.getcolor (R.styleable.custompagertab_textcolor, TextColor);            TextSize = Attrarray.getdimensionpixelsize (r.styleable.custompagertab_textsize, textSize);        Attrarray.recycle ();        } settextcolor (TextColor);    Settextsize (typedvalue.complex_unit_sp, textSize); }////pagertabstrip constructors with no three parameters//public Pagertab (context context, AttributeSet attrs, int defstyleattr) {//} }

3. The root node of the layout file adds a custom namespace declaration, such as "xmlns:app=" Http://schemas.android.com/apk/res-auto "" and change the Android.support.v4.view.PagerTabStrip node name to the full path name of the custom view, such as "Com.example.custom.widget.PagerTab", At the same time, the new two attributes, App:textcolor and app:textsize, are specified under this node. The layout file code after the modification is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:app= "http// Schemas.android.com/apk/res-auto "    android:layout_width=" match_parent "    android:layout_height=" Match_ Parent "    android:orientation=" vertical "    android:padding=" 10DP ">    < Android.support.v4.view.ViewPager        android:id= "@+id/vp_content"        android:layout_width= "Match_parent"        android:layout_height= "400DP" >        <com.example.custom.widget.custompagertab            android:id= "@+id/ Pts_tab "            android:layout_width=" wrap_content "            android:layout_height=" wrap_content "            App:textcolor = "@color/red"            app:textsize= "17sp"/>    </android.support.v4.view.viewpager></linearlayout >

The three steps of the custom attribute above, where the second step involves Java code, and then overwrites the code of the Custompagertab class with Kotlin, the main changes are the following two points:

1, the original two constructors, merged into a primary constructor with default parameters, and directly followed by the class name;
2, followed by the annotated "@JvmOverloads constructor" after the class name, indicating that the class supports being called by Java code. Because the layout file references the node of the custom view, the system uses the Java code in the SDK to find the custom view class, so any custom view should add that annotation, or the app will throw an exception when it runs.
The following is the Kotlin code after the Custompagertab class is overwritten:

Custom view Be sure to add "@JvmOverloads constructor" after the class name, because the custom view in the layout file must be compatible with Javaclass Custompagertab @JvmOverloads Constructor ( Context:context, Attrs:attributeset?=null): Pagertabstrip (Context, attrs) {    private var txtcolor = color.black
   
    private var textSize =        init {        txtcolor = color.black        textSize =/        / When initializing, read the Custompagertab custom property from Attrs.xml        if (attrs! = null) {            val Attrarray = GetContext (). obtainstyledattributes (Attrs, R.styleable.custompagertab)            Txtcolor = Attrarray.getcolor (R.styleable.custompagertab_textcolor, txtcolor)            textSize = Attrarray.getdimensionpixelsize (R.styleable.custompagertab_textsize, textSize)            attrarray.recycle ()        }        SetTextColor (Txtcolor)        settextsize (typedvalue.complex_unit_sp, Textsize.tofloat ())    }}
   

After completing the three-step modification, run the test application and display the interface effect as shown, and the text color of the page title bar turns red, and the font size becomes larger.

Getting Started with Kotlin (24) How to customize a view

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.