• 自定义控件——视图的构建过程——视图的构造方法


    视图基类View有四个构造方法,分别是:

    (1)带一个参数的构造方法public View(Context context)(2)带两个参数的构造方法public View(Context context, AttributeSet attrs)(3)带三个参数的构造方法public View(Context context, AttributeSet attrs, int defStyleAttr)(4)带四个参数的构造方法public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

    构造方法的四个参数说明如下:(1)Context context,视图的上下文(2)AttributeSet attrs,视图的属性设置(3)int defStyleAttr,默认的样式属性(4) int defStyleRes,默认的样式资源

    styles.xml定义的样式风格允许用在多个地方,这些地方的优先级顺序为:style属性>defStyleAttr>defStyleRes

    ===========================================================================================

    第一个,系统默认的button

    1. <Button
    2. android:layout_width="match_parent"
    3. android:layout_height="wrap_content"
    4. android:text="这是系统默认的Button"/>

    第二个,来自style的button

    第三个,自定义样式

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <Button
    6. android:layout_width="match_parent"
    7. android:layout_height="wrap_content"
    8. android:text="这是系统默认的Button"/>
    9. <Button
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:text="这是来自style的Button"
    13. style="@style/CommonButton"/>
    14. <!-- 注意自定义控件需要指定该控件的完整路径 -->
    15. <com.example.myapplication.widget.CustomButton
    16. android:layout_width="match_parent"
    17. android:layout_height="wrap_content"
    18. android:text="这是自定义的Button"
    19. android:background="#ffff00"/>
    20. </LinearLayout>

    第三个参数:defStyleAttr,指定默认的样式属性:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3. <declare-styleable name="CustomButton">
    4. <attr name="customButtonStyle" format="reference" />
    5. </declare-styleable>
    6. </resources>

    1. <resources>
    2. <!-- Base application theme. -->
    3. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    4. <!-- Customize your theme here. -->
    5. <item name="colorPrimary">@color/colorPrimary</item>
    6. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    7. <item name="colorAccent">@color/colorAccent</item>
    8. <item name="customButtonStyle">@style/CommonButton</item>
    9. </style>
    10. <style name="CommonButton">
    11. <item name="android:textAllCaps">false</item>
    12. <item name="android:textColor">#000000</item>
    13. <item name="android:textSize">20sp</item>
    14. </style>
    15. </resources>

    代码:

    1. package com.example.myapplication.widget;
    2. import android.annotation.SuppressLint;
    3. import android.content.Context;
    4. import android.util.AttributeSet;
    5. import android.util.Log;
    6. import android.widget.Button;
    7. import com.example.myapplication.R;
    8. @SuppressLint("AppCompatCustomView")
    9. public class CustomButton extends Button
    10. {
    11. private final static String TAG = "CustomButton";
    12. public CustomButton(Context context)
    13. {
    14. super(context);
    15. Log.d(TAG, "只有一个输入参数");
    16. }
    17. public CustomButton(Context context, AttributeSet attrs)
    18. {
    19. this(context, attrs, R.attr.customButtonStyle);
    20. Log.d(TAG, "有两个输入参数");
    21. }
    22. public CustomButton(Context context, AttributeSet attrs, int defStyleAttr)
    23. {
    24. // 使defStyleAttr奏效的三个条件:
    25. // 1、attrs.xml增加定义风格属性(如customButtonStyle),且format值为reference
    26. // 2、styles.xml增加某种风格的样式定义(如CommonButton)
    27. // 3、AndroidManifest.xml的application节点的android:theme指定了哪个主题(如AppTheme),就在该主题内部补充风格属性与样式的对应关系,如
    28. // @style/CommonButton
    29. //super(context, attrs, defStyleAttr); // 设置默认的样式属性
    30. // 下面不使用defStyleAttr,直接使用R.style.CommonButton定义的样式
    31. this(context, attrs, 0, R.style.CommonButton);
    32. Log.d(TAG, "有三个输入参数");
    33. }
    34. @SuppressLint("NewApi")
    35. public CustomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    36. {
    37. super(context, attrs, defStyleAttr, defStyleRes);
    38. Log.d(TAG, "有四个输入参数");
    39. }
    40. }

     

  • 相关阅读:
    测试开发:10分钟Flask快速入门【建议收藏】
    Qt信号和槽 定时器
    localhost与127.0.0.1和本机ip的区别
    前端面试宝典React篇13 如何分析和调优性能瓶颈?
    Day18ACL
    H3C配置mstp参数,优化收敛时间
    01_中间件
    手把手教会你|Sockets多用户-服务器数据库编程
    下划线命名转驼峰
    【读博日记】拓扑结构(待修正)
  • 原文地址:https://blog.csdn.net/m0_61442607/article/details/126678749