
我们,先做一下简单的分析一下,这是一个自定义控件,应该像Android的原生控件一样,能够方便调用者设置控件的属性。因此,十分有必要为这个控件设置一些属性,为一个View提供一些自定义属性十分的简单,只需要在res资源目录下的values目录下创建一个attrs.xml属性文件,并在该文件定义你所需要的属性即可。这个自定义控件自定义属性如下:
<declare-styleable name="titleBar"><attr name="title" format="string" /><attr name="titleTextSize" format="dimension" /><attr name="titleTextColor" format="color" /><attr name="titleLeftText" format="string" /><attr name="titleLeftBackground" format="color|reference" /><attr name="titleLeftTextColor" format="color" /><attr name="titleRightText" format="string" /><attr name="titleRightBackground" format="color|reference" /><attr name="titleRightTextColor" format="color" /> </declare-styleable>我们用<declare-styleable>标签声明要使用的自定义属性,用name属性来确定引用的名称。用format来确定引用数据的格式。在这个自定义控件自定义属性对应列表如下:
private void initAttrs(AttributeSet attrs) {TypedArray ta = this.getContext().obtainStyledAttributes(attrs,R.styleable.titleBar);if (ta != null) { title = ta.getString(R.styleable.titleBar_title); titleTextSize = ta.getDimension(R.styleable.titleBar_titleTextSize, 16); titleTextColor = ta .getColor(R.styleable.titleBar_titleTextColor, 0); titleLeftText = ta.getString(R.styleable.titleBar_titleLeftText); titleLeftBackground = ta .getDrawable(R.styleable.titleBar_titleLeftBackground); titleLeftTextColor = ta.getColor( R.styleable.titleBar_titleLeftTextColor, 0); titleRightText = ta.getString(R.styleable.titleBar_titleRightText); titleRightBackground = ta .getDrawable(R.styleable.titleBar_titleRightBackground); titleRightTextColor = ta.getColor( R.styleable.titleBar_titleRightTextColor, 0); ta.recycle();} } 这里,需要值得一提的是需要调用TypedArray的recycle方法将资源回收。 private void initView() {leftButton = new Button(getContext());titleTextView = new TextView(getContext());rightButton = new Button(getContext());leftButton.setTextColor(titleLeftTextColor);leftButton.setBackgroundDrawable(titleLeftBackground);leftButton.setText(titleLeftText);rightButton.setTextColor(titleRightTextColor);rightButton.setBackgroundDrawable(titleRightBackground);rightButton.setText(titleRightText);titleTextView.setText(title);titleTextView.setTextSize(titleTextSize);titleTextView.setTextColor(titleTextColor);mLeftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);mLeftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);addView(leftButton, mLeftLayoutParams);mCenterLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);mCenterLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);addView(titleTextView, mCenterLayoutParams);mRightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);mRightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);addView(rightButton, mRightLayoutParams); } public interface ClickListener {void Click(int tag); } private ClickListener listener; 在模版方法中,为左、右按钮增加点击事件,调用接口的点击方法,代码如下所示:private void setListener() {leftButton.setOnClickListener(this);rightButton.setOnClickListener(this); } @Override public void onClick(View v) {if (listener != null) { if (v == leftButton) {listener.Click(LEFT_BUTTON); } else if (v == rightButton) {listener.Click(RIGHT_BUTTON); }} } 在代码,我们有效判断是左边按钮点击了,还是右边按钮点击了。 titleBar.setListener(new ClickListener() {@Override public void Click(int tag) {switch (tag) { case TitleBar.LEFT_BUTTON:Toast.makeText(MainActivity.this, "左边按钮被点击了", 0).show();break; case TitleBar.RIGHT_BUTTON:Toast.makeText(MainActivity.this, "右边按钮被点击了", 0).show();break; default:break; }}}); 这样在外部,能够有效的控制左右按钮的点击事件了。<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res/com.example.test" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context=".MainActivity"> <!-- <include layout="@layout/topbar" /> --> <com.example.test.TitleBarandroid:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="40dp"custom:titleLeftBackground="@drawable/blue_button"custom:titleLeftText="Back"custom:titleLeftTextColor="#FFFFFF"custom:titleRightBackground="@drawable/blue_button"custom:titleRightText="More"custom:titleRightTextColor="#FFFFFF"custom:title="自定义标题"custom:titleTextColor="#123412"custom:titleTextSize="10sp"/></RelativeLayout>这里,需要和大家交代的是,自定义控件与原生控件调用区别在于:

以上就是本文的全部内容,希望对大家的学习有所帮助。