美文网首页Android开发Android技术知识Android开发
自定义控件(继承系统控件,非自绘)

自定义控件(继承系统控件,非自绘)

作者: 手指乐 | 来源:发表于2019-08-21 21:33 被阅读12次
  1. 写一个类继承已有的控件(比如textview),override其中的构造函数:
    一般重写这两个构造函数:
public class MyTextView extends TextView {
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
   }

一个构造函数一般在代码里new这个自定义控件时使用

第二个构造函数是在layout的xml文件中声明这个控件时,自动调用,attrs里包含了在xml中给这个自定义view设置的所有属性,包括自定义属性和系统属性

  1. 在xml中声明这个自定义控件,并设置相关属性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.zhouyi.userdefineattrsandstyle.MainActivity">

    <com.example.zhouyi.userdefineattrsandstyle.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>
  1. 这时候,可以在其构造函数中获取这些定义的系统属性:
public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray typedArray = context.obtainStyledAttributes(attrs,
            new int[]{android.R.attr.text});
    String strValue = typedArray.getString(0);
}

obtainStyledAttributes用于在所有属性集合(attrs)中获取我们需要的属性值,第二个参数是一个int数组,里面设置我们需要获取的属性对应的资源id,比如textview的text属性其实是在android的系统资源的属性文件中设置的一个属性

  1. 使用自定义属性:
    首先在res/values底下建立一个attrs.xml文件,指定自定义属性集合的名字,以及里面的属性名和属性格式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTextViewStyle">
        <attr name="attr_one" format="string"/>
        <attr name="attr_two" format="string" />
        <attr name="attr_three" format="string" />
        <attr name="attr_four" format="string" />
    </declare-styleable>
</resources>

在xml中定义自定义属性的命名空间并设置自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:test = "http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.zhouyi.userdefineattrsandstyle.MainActivity">

    <com.example.zhouyi.userdefineattrsandstyle.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        test:attr_one="attr_one from xml"
        android:text="Hello World!" />
</RelativeLayout>

自定属性的命名空间名字可以随意,值统一为:http://schemas.android.com/apk/res-auto

在代码中获取自定义属性值:

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray typedArray = context.obtainStyledAttributes(attrs,
            R.styleable.MyTextViewStyle);

    String strValue_one = typedArray.getString(R.styleable.MyTextViewStyle_attr_one);
}

在样式(style.xml)中给自定义属性赋值,不需要android:xxx,直接写属性名即可

<style name="ThroughStyle">
    <item name="attr_one">attr one from style</item>
    <item name="attr_two">attr two from style</item>
</style>

相关文章

  • 自定义View记录

    自定义View只要有三种类型:自绘控件、组合控件、继承控件。 自绘控件 自定义View主要是因为系统的内置View...

  • 自定义控件的实现

    自定义控件可以按类型分三种,自绘控件、组合控件和继承控件 自绘控件:View上所展示的所有内容都是我们自己写在on...

  • 自定义控件(继承系统控件,非自绘)

    写一个类继承已有的控件(比如textview),override其中的构造函数:一般重写这两个构造函数: 一个构造...

  • 购物车加减器自定义组合控件

    一.案例效果 二.技术点描述: 1.自绘控件(完全自定义控件):继承的是RelativeLayout2.自定义控件...

  • 自定义View时,使用id值的正确姿势

    假设: 存在继承自View的自定义控件CView; 存在继承自ViewGroup的自定义控件CViewGroup;...

  • 自定义日历控件

    Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置...

  • 自定义控件(通过代码或者xib文件)

    一、通过代码来自定义控件 继承自系统自带的控件,写一个属于自己的控件 目的:封装控件内部的细节,不让外界关心 步骤...

  • 自定义View

    1、组合控件 2、自绘控件 3、继承已有控件 用到的技能: 1、自定义属性 2、根据需要设置接口回调 复习一下那个...

  • winform自制控件

    1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserContr...

  • 自定义控件笔记1

    自定义控件:用系统自带控件重新组合或者自定义类继承View或者自定义类继承ViewGroup,实现特定的UI效果。...

网友评论

    本文标题:自定义控件(继承系统控件,非自绘)

    本文链接:https://www.haomeiwen.com/subject/jbmisctx.html