自定义控件属性

demo:defineView

1.如何自定义控件属性?

2.如何动态创建组件?
3.接口回调思想
 
设计需要的属性
values新建attrs.xml。通过<declare-styleable>来告诉系统这是自定义的属性
  1. <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleablename="Topbar">
    <attrname="title"format="string"/>
    <attrname="titleSize"format="dimension"/>
    <attrname="titleTextColor"format="color"/>
    <attrname="leftTextColor"format="color"/>
    <attrname="leftBackground"format="reference|color"/>
    <attrname="leftText"format="string"/>
    <attrname="rightTextColor"format="color"/>
    <attrname="rightBackground"format="reference|color"/>
    <attrname="rightText"format="string"/>
    </declare-styleable>
    </resources>
实现一个自定义的“View”
重写构造方法,在构造方法中取出自定义值存到TapeArray中,从TypeArray中取出相应的值给了相应的变量,然后组合控件并添加到
ViewGroup中,所有布局属性都在里面设置。
  1. public class TopbarextendsRelativeLayout{
        private Button leftButton,rightButton;
        private TextView tvText;
       /**
       * 左边字体的颜色
       */
       private int leftTextColor;
       /**
       * 左边背景的颜色
       */
       private Drawable leftBackground;
       /**
       * 左边butto文字
       */
       private String leftText;
       /**
       * 右边字体的颜色
       */
       private int rightTextColor;
       /**
       * 右边背景的颜色
       */
       private Drawable rightBackground;
       /**
       * 右边butto文字
       */
       private String rightText;
       /**
       * title要显示文字的大小
       */
       private float titleTextSize;
       /**
       * title字体的颜色
       */
       private int titleTextColor;
       /**
       * title的文字
       */
       private String title;
       private LayoutParams leftParams,rightParams,titleParams;
       private topbarClickListener listener;
       /**
       * 实现自己的接口
       * @author Kevin
       *
       */
       public interface topbarClickListener{
       public void leftClick();
       public void rightClick();
    }
      /**
      * 将传进来的映射
      * @param listener
      */
      public void setOnTopClickListener(topbarClickListener listener){
        this.listener = listener;
      }
      //需要自定义属性,所以需要attrs参数
     public Topbar(Context context,AttributeSet attrs){
       super(context, attrs);
       //存取自定义属性中的值返回到TypedArray
       TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);
       //完成属性赋值
       leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor,0);
       leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
       rightText = ta.getString(R.styleable.Topbar_rightText);
       rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor,0);
       rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
       leftText = ta.getString(R.styleable.Topbar_leftText);
       titleTextSize = ta.getDimension(R.styleable.Topbar_titleSize,0);
       titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor,0);
       title = ta.getString(R.styleable.Topbar_title);
       //TypedArray一定要回收,避免浪费资源和由于缓存造成的错误
       ta.recycle();
       leftButton =newButton(context);
       rightButton =newButton(context);
       tvText =newTextView(context);
       //自定义属性赋值给自定义的控件
       leftButton.setTextColor(leftTextColor);
       leftButton.setBackgroundDrawable(leftBackground);
       leftButton.setText(leftText);
       rightButton.setTextColor(rightTextColor);
       rightButton.setBackgroundDrawable(rightBackground);
       rightButton.setText(rightText);
       tvText.setTextColor(titleTextColor);
       tvText.setTextSize(titleTextSize);
       tvText.setText(title);
       tvText.setGravity(Gravity.CENTER);
       setBackgroundColor(0xFFF59563);
       //设置布局参数
       leftParams =newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
       //居左对齐 
       leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
       //以leftParams的方式添加进去
       addView(leftButton,leftParams);
       //设置布局参数
       rightParams =newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
       //居右对齐
       rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
       addView(rightButton,rightParams);
       titleParams =newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
       titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
       addView(tvText,titleParams);
       leftButton.setOnClickListener(newOnClickListener(){
       @Override
       public void onClick(View v){
           listener.leftClick();
         }
       });
      rightButton.setOnClickListener(newOnClickListener(){
      @Override
       public void onClick(View v){
         listener.rightClick();
       }
      });
     }
    }

     

接口回调抽象了使用的方法,点击事件抽象了出来,让调用者决定到底做什么,topbar就被封装起来了。
引用“View”
xmlns是命名空间,需要添加命名空间来引用自己的属性,AS中auto就行,Eclipse需要完整的包名。
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.llc.defineview"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.llc.defineview.MainActivity">
      <com.llc.defineview.Topbar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        custom:leftBackground="@drawable/back_bg"
        custom:leftText="back"
        custom:leftTextColor="#ffffff"
        custom:rightBackground="@drawable/edit_bg"
        custom:rightText="more"
        custom:rightTextColor="#ffffff"
        custom:title="自定义标题"
        custom:titleTextColor="#123421"
        custom:titleSize="10sp">
       </com.llc.defineview.Topbar>
    </RelativeLayout>
MainActivity.java
  1. publicclassMainActivityextendsActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Topbar topbar =(Topbar) findViewById(R.id.topbar);
        topbar.setOnTopClickListener(newTopbar.topbarClickListener(){
        @Override
        publicvoid rightClick(){
            Toast.makeText(MainActivity.this,"left",Toast.LENGTH_LONG).show();
        }
        @Override
        publicvoid leftClick(){
           Toast.makeText(MainActivity.this,"right",Toast.LENGTH_LONG).show();
         }
       });
     }
    }

     

 
 



posted @ 2015-01-27 01:38  咖啡馆的水果拼盘  阅读(209)  评论(0编辑  收藏  举报