• Android自定义组合控件


    自定义组合控件为自定义控件中的一种,由已有的控件组合而成。自定义控件类需要继承已有控件(RelativeLayout 、LinearLayout等)

    1.组合控件的XML布局文件( res/layout/xxx.xml )

    1. "1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content">
    5. RelativeLayout>

    2.设置组合控件的属性的XML文件 ( res/value/attrs.xml )

    1. "1.0" encoding="utf-8"?>
    2. <resources>
    3. <declare-styleable name="MyView">
    4. <attr name="attrN1" format="string"/>
    5. <attr name="attrN2">
    6. <flag name="FLAG_N1" value="1"/>
    7. <flag name="FLAG_N2" value="1"/>
    8. attr>
    9. declare-styleable>
    10. resources>

    declare-声明  styleable-风格  attr-属性  flag-标识、标志

    format="reference"标识类型为资源 ( R.~.~ )

    3.创建自定义组合控件类( app / java / 包名 / xxx.java ) 

    1. public class MyView extends RelativeLayout { //继承已有控件(RelativeLayout、LinearLayout等)
    2. private int i;
    3. private TextView textView;
    4. public MyView(Context context) {
    5. super(context);
    6. //用于Java文件中的构造函数
    7. }
    8. public MyView(Context context, AttributeSet attrs) {
    9. super(context, attrs);
    10. //用于XML布局文件中的构造函数
    11. getAttrs(context,attrs);
    12. setView(context);
    13. }
    14. private void getAttrs(Context context,AttributeSet attrs){
    15. //获取属性值
    16. TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
    17. i=typedArray.getInt(R.styleable.MyView_attrN2,1);
    18. int id=typedArray.getResourceId(R.styleable.MyView_attrN3,R.~.~);
    19. //typedArray.getString typedArray.getBoolean
    20. //回收
    21. typedArray.recycle();
    22. }
    23. private void setView(Context context){
    24. LayoutInflater.from(context).inflate(R.layout.myview,this);
    25. textView=(TextView) findViewById( ~ );
    26. textView.setText(i);
    27. }
    28. }

    获取自定义控件属性值以后一定要回收TypedArray

    使用getResourceId()获取类型为reference的资源ID

    自定义组合控件需要继承已有控件(RelativeLayout、LinearLayout等)

    LayoutInflater.from( context ).inflate( R.layout.myview , this ); 根为this

    4.使用自定义组合控件

    1. <MyView
    2. app:attrN1="testString"
    3. app:attrN2="FLAGN1"
    4. ... ...
    5. />

    自定义属性使用:  app: 属性名 = " 属性值 " 

    5.注:自定义控件类需实现方法

    1. public class MyView extends RelativeLayout {
    2. public MyView(Context context) {
    3. super(context);
    4. //用于Java文件中的构造函数
    5. }
    6. public MyView(Context context, AttributeSet attrs) {
    7. super(context, attrs);
    8. //用于XML布局文件中的构造函数
    9. TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyView);
    10. LayoutInflater.from(context).inflate(R.layout.myview,this);
    11. }
    12. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    13. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    14. //该方法用于测量尺寸,在该方法中可以设置控件本身或其子控件的宽高
    15. }
    16. protected void onDraw(Canvas canvas) {
    17. super.onDraw(canvas);
    18. //该方法用于绘制图像
    19. }
    20. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    21. super.onLayout(changed, left, top, right, bottom);
    22. //用于指定布局中子控件的位置
    23. }
    24. }

  • 相关阅读:
    vue3中自定义Ref
    java中Array(数组)、List(列表)、Set(集合)、Map(映射)、Queue(队列)详解
    同城服务系统商家商城城市代理躲猫猫小程序
    Java基础-并发篇
    【PHP代码审计】——开启你的代码审计生涯
    4.2 Ioc容器加载过程-Bean的生命周期深度剖析
    PFSK165 3BSE027778R1 VP74201-933CW07 允许用户对数据进行读写操作
    RK3399平台开发系列讲解(中断篇)13.16、request_irq的实现
    cesium切片底图正常出来但控制台一直报错的方法
    深度融入垂直行业是物联网未来发展必由之路
  • 原文地址:https://blog.csdn.net/m0_57150356/article/details/133950254