• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android 自定义 View - 四个构造函数

武飞扬头像
teletian
帮助1

View 的构造函数有四个:

  1. public View(Context context)
  2. public View(Context context, @Nullable AttributeSet attrs)
  3. public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
  4. public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)

我们在自定义 View 继承 View 的时候,1 和 2 是必须重写的。

第 1 个构造函数是在 java 代码中声明一个 View 时所用。
第 2 个构造函数是在布局文件中声明一个 View 时所用。参数 attrs 可以获取在布局文件中定义的 View 的属性值。

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
    String text = ta.getString(R.styleable.CustomTextView_text);
    ta.recycle();  

构造函数 3 和 4 是可选的,

在讲这两个构造函数之前,我们先来复习一下属性主题
View 有很多属性,我们有以下 5 种方法给这些属性赋值:

  1. xml 中直接定义
    <TextView
        android:textSize="40sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  1. xml 中 style 引用
    <style name="MyTextViewStyle">
        <item name="android:textSize">40sp</item>
        <item name="android:textColor">@color/black</item>
    </style>
    <TextView
        style="@style/MyTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  1. theme 中直接定义
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ......
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">40sp</item>
    </style>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.sample.xxx">

    <application
        ......
        android:theme="@style/Theme.MyApplication">
        ......
    </application>

</manifest>

主题会应用到所有的 View,作为默认的值,即使 View 没有明确定义某个属性,也会给 View 添加属性的值。

  1. defStyleAttr:第 3 个和 第 4 个构造函数中的参数

先来看下源码中的解释。
An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
可以看到,defStyleAttr 是在主题中定义的,是一个指向 style 的 reference。

    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ......
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    </style>

其实吧,这种方式和 2 是类似的,最终都是指定一个 style,只不过这种方式通过在主题中添加了一个属性,然后通过指定属性来间接使用 style。
系统的 TextView 就用到了 defStyleAttr。

public TextView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

textViewStyle 定义在 platforms/android-xx/data/res/values 的 attrs.xml 中

<declare-styleable name="Theme">
  ......
  <attr name="textViewStyle" format="reference"/>
  ......
</declare-styleable>
  1. defStyleRes:第 4 个构造函数中的参数

这种方式很简单,和 2 类似,使用 style。只不过 2 是 xml 布局文件中使用,这里是 Java 代码中使用。
这里要注意一下,defStyleAttr 的优先级要高于 defStyleRes,只有当 defStyleAttr 为 0 的时候,才有可能(有可能是因为 1 2 3 的方式优先级更高)使用 defStyleRes。

那么这 5 种方式的优先级是什么样子的呢?
先公布答案:xml 中直接定义 > xml 中 style 引用 > defStyleAttr > defStyleRes > theme 中直接定义

下面通过一个例子验证:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <com.teletian.sample.myapplication.CustomTextView
        style="@style/MyTextViewStyle"
        android:textColor="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
学新通

CustomTextView.java

@SuppressLint("AppCompatCustomView")
public class CustomTextView extends TextView {

   public CustomTextView(Context context) {
      this(context, null);
   }

   public CustomTextView(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, R.attr.textViewColorStyle);
   }

   public CustomTextView(Context context, @Nullable AttributeSet attrs,
                         int defStyleAttr) {
      this(context, attrs, defStyleAttr, R.style.MyTextViewStyle3);
   }

   public CustomTextView(Context context, @Nullable AttributeSet attrs,
                         int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
   }
}
学新通

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textViewColorStyle" format="reference" />
</resources>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTextViewStyle">
        <item name="android:textColor">@android:color/holo_blue_dark</item>
    </style>
    <style name="MyTextViewStyle2">
        <item name="android:textColor">@android:color/holo_green_dark</item>
    </style>
    <style name="MyTextViewStyle3">
        <item name="android:textColor">@android:color/holo_orange_dark</item>
    </style>
</resources>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ......
        <item name="android:textColor">@android:color/holo_red_dark</item>
        <item name="textViewColorStyle">@style/MyTextViewStyle2</item>
    </style>
</resources>

可以看到,我们给 5 种方式都设置了 textColor。
xml 中直接定义(黑色) > xml 中 style 引用(蓝色) > defStyleAttr(绿色) > defStyleRes(橙色) > theme 中直接定义(红色)

依次运行,看结果

  1. 运行,文字黑色

  2. 删掉 xml 中的直接定义(android:textColor=“@android:color/black”),运行,文字蓝色

  3. 继续删掉 xml 中 style 引用(style=“@style/MyTextViewStyle”),运行,文字绿色

  4. 继续将 CustomTextView 中的 defStyleAttr 设为 0

   public CustomTextView(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, 0);
   }

运行,文字橙色

  1. 继续将 CustomTextView 中的 defStyleRes 设为 0
   public CustomTextView(Context context, @Nullable AttributeSet attrs,
                         int defStyleAttr) {
      this(context, attrs, defStyleAttr, 0);
   }

运行,文字红色

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfaebi
系列文章
更多 icon
同类精品
更多 icon
继续加载