自定义控件思路:
1、了解View的工作原理
2、 编写继承自View的子类
3、 为自定义View类增加属性
4、 绘制控件
5、 响应用户消息
6 、自定义回调函数
参考链接:http://www.cnblogs.com/0616--ataozhijia/p/4003380.html
不过本文是自定义组合控件,其实也都差不多。自定义控件MyTopBar继承自RelativeLayout,在带有两个参数的构造方法中初始化了TopBar的一些子控件,
先看MainActivity中如何调用:
package com.example.mytopbar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.mytopbar.TopBar.TopbarClickListener;
public class MainActivity extends Activity {
private TopBar topBar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topBar = (TopBar) findViewById(R.id.topbar);
topBar.setTopBarClickListener(new TopbarClickListener() {
@Override
public void rightClick() {
Toast.makeText(MainActivity.this, "right click", Toast.LENGTH_SHORT).show();
}
@Override
public void leftClick() {
Toast.makeText(MainActivity.this, "left click", Toast.LENGTH_SHORT).show();
}
});
topBar.setLeftVisible(false);
}
}
如下是MyTopBar的代码:
package com.example.mytopbar;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class TopBar extends RelativeLayout {
private Button leftButton,rightButton;
private TextView tvTitle;
private int leftTextColor = 0;
private Drawable leftBackground = null;
private String leftText;
private int rightTextColor = 0;
private Drawable rightBackground = null;
private String rightText;
private float titleTextSize;
private int titleTextColor;
private String title;
private RelativeLayout.LayoutParams leftParams,rightParams,titleParams;
private TopbarClickListener listener;
public interface TopbarClickListener{
public void leftClick();
public void rightClick();
}
public void setTopBarClickListener(TopbarClickListener listener){
this.listener = listener;
}
public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@SuppressLint("NewApi")
public TopBar(Context context, AttributeSet attrs) {
this(context,attrs,0);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
leftText = ta.getString(R.styleable.TopBar_leftText);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
rightText = ta.getString(R.styleable.TopBar_rightText);
rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
title = ta.getString(R.styleable.TopBar_title);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 0);
ta.recycle();
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
leftButton.setTextColor(leftTextColor);
leftButton.setText(rightText);
leftButton.setBackground(leftBackground);
rightButton.setBackground(rightBackground);
rightButton.setTextColor(rightTextColor);
rightButton.setText(rightText);
tvTitle.setText(title);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xFFF59563);
leftParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
this.addView(leftButton, leftParams);
rightParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
this.addView(rightButton, rightParams);
titleParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
this.addView(tvTitle, titleParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
//Toast.makeText(context, "Tommy Left", Toast.LENGTH_SHORT).show();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
//Toast.makeText(context, "Tommy Right", Toast.LENGTH_SHORT).show();
}
});
}
public TopBar(Context context) {
this(context,null,0);
}
public void setLeftVisible(boolean flag){
if(flag){
this.leftButton.setVisibility(View.VISIBLE);
}else{
this.leftButton.setVisibility(View.GONE);
}
}
}
Values目录下的attrs.xml文件为自定义的属性,以下是attrs.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="title" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
</declare-styleable>
</resources>
下面在activity_main.xml中引用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tommy="http://schemas.android.com/apk/res/com.example.mytopbar"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity" >
<com.example.mytopbar.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
tommy:leftBackground="@drawable/ic_launcher"
tommy:leftText="Back"
tommy:leftTextColor="#FFFFFF"
tommy:rightBackground="@drawable/ic_launcher"
tommy:rightText="More"
tommy:rightTextColor="#FFFFFF"
tommy:title="自定义标题"
tommy:titleTextColor="#123412"
tommy:titleTextSize="10sp"
>
</com.example.mytopbar.TopBar>
</RelativeLayout>