Kotlin入门(24)如何自定义视图

时间:2022-06-17
本文章向大家介绍Kotlin入门(24)如何自定义视图,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Android提供了丰富多彩的视图与控件,已经能够满足大部分的业务需求,然而计划赶不上变化,总是有意料之外的情况需要特殊处理。比如PagerTabStrip无法在布局文件中指定文本大小和文本颜色,只能在代码中通过setTextSize和setTextColor方法来设置。这用起来殊为不便,如果它能像TextView那样直接在布局指定文本大小和颜色就好了;要想让PagerTabStrip支持该特性,就得通过自定义视图来实现,而自定义视图的第一种途径便是自定义属性。 仍旧以翻页标题栏PagerTabStrip举例,现在给它新增两个自定义属性,分别是文本颜色textColor,以及文本大小textSize。下面给出Java编码的自定义步骤: 1. 在resvalues目录下创建attrs.xml,文件内容如下所示,其中declare-styleable的name属性值表示新视图的名称,两个attr节点表示新增的两个属性分别是textColor和textSize:

<resources>
    <declare-styleable name="CustomPagerTab">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

2. 在模块的widget目录下创建CustomPagerTab.java,填入以下自定义视图的代码:

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);
        //构造函数从attrs.xml读取CustomPagerTab的自定义属性
        if (attrs != null) {
            TypedArray attrArray=getContext().obtainStyledAttributes(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没有三个参数的构造函数
//    public PagerTab(Context context, AttributeSet attrs, int defStyleAttr) {
//    }
}

3. 布局文件的根节点增加自定义的命名空间声明,如“xmlns:app="http://schemas.android.com/apk/res-auto"”;并把android.support.v4.view.PagerTabStrip的节点名称改为自定义视图的全路径名称如“com.example.custom.widget.PagerTab”,同时在该节点下指定新增的两个属性即app:textColor与app:textSize。修改之后的布局文件代码如下:

<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>

上述自定义属性的三个步骤,其中第二步骤涉及到Java代码,接下来用Kotlin改写CustomPagerTab类的代码,主要改动有以下两点: 1、原来的两个构造函数,合并为带默认参数的一个主构造函数,并且直接跟在类名后面; 2、类名后面要加上注解“@JvmOverloads constructor”,表示该类支持被Java代码调用。因为布局文件中引用了自定义视图的节点,系统是通过SDK里的Java代码找到自定义视图类,所以凡是自定义视图都要加上该注解,否则App运行时会抛出异常。 下面是CustomPagerTab类改写之后的Kotlin代码:

//自定义视图务必要在类名后面增加“@JvmOverloads constructor”,因为布局文件中的自定义视图必须兼容Java
class CustomPagerTab @JvmOverloads constructor(context: Context, attrs: AttributeSet?=null) : PagerTabStrip(context, attrs) {
    private var txtColor = Color.BLACK
    private var textSize = 15
    
    init {
        txtColor = Color.BLACK
        textSize = 15
        //初始化时从attrs.xml读取CustomPagerTab的自定义属性
        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())
    }
}

完成以上三步修改后,运行测试应用,展示的界面效果如下图所示,此时翻页标题栏的文字颜色变为红色,而且字体也变大了。

点此查看Kotlin入门教程的完整目录