当前位置:   article > 正文

Android自定义组合控件

android自定义组合控件

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

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

  1. <?xml version="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. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 声明-风格 自定义控件名-->
  4. <declare-styleable name="MyView">
  5. <attr name="attrN1" format="string"/>
  6. <!-- 属性名 属性类型 -->
  7. <attr name="attrN2">
  8. <flag name="FLAG_N1" value="1"/>
  9. <!-- 属性名 属性值(此值只能为int) -->
  10. <flag name="FLAG_N2" value="1"/>
  11. </attr>
  12. </attr name="attrN3" format="reference"/>
  13. <!-- 属性名 属性值(资源R.~.~) -->
  14. </declare-styleable>
  15. </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. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/759720
推荐阅读
相关标签
  

闽ICP备14008679号