//获取typedarray里面的属性
private void initAttrs(AttributeSet attrs) {
TypedArray ta = ctx.obtainStyledAttributes(attrs, R.styleable.TopTitle);
title = ta.getString(R.styleable.TopTitle_title);
titleTextColor = ta.getColor(R.styleable.TopTitle_titleTextColor, 0);
titleTextSize = ta.getDimensionPixelSize(R.styleable.TopTitle_titleTextSize, 12);
leftText = ta.getString(R.styleable.TopTitle_leftText);
rightText = ta.getString(R.styleable.TopTitle_rightText);
leftBackgroud = ta.getDrawable(R.styleable.TopTitle_leftBackgroud);
rightBackgroud = ta.getDrawable(R.styleable.TopTitle_rightBackgroud);
}
//分别为标题显示、左右两个button
tvTitle = new TextView(ctx);
leftBtn = new Button(ctx);
righttBtn = new Button(ctx);
//设置属性值
tvTitle.setText(title);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);
leftBtn.setText(leftText);
leftBtn.setBackgroundDrawable(leftBackgroud);
righttBtn.setText(rightText);
righttBtn.setBackgroundDrawable(rightBackgroud);
//控制显示位置
LayoutParams titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle, titleParams);
//控制显示位置
LayoutParams leftBtnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
leftBtnParams.addRule(RelativeLayout.ALIGN_LEFT, TRUE);
addView(leftBtn, leftBtnParams);
//控制显示位置
LayoutParams rightBtnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rightBtnParams.addRule(RelativeLayout.ALIGN_RIGHT, TRUE);
addView(righttBtn, rightBtnParams);
//为两个按钮设置监听,通过接口方式对外提供
leftBtn.setTag(LEFT_BUTTON);
righttBtn.setTag(RIGHT_BUTTON);
leftBtn.setOnClickListener(this);
righttBtn.setOnClickListener(this);
public void setOnToptitleClickListener(ToptitleClickListener listener) {
this.mToptitleClickListener = listener;
}
@Override
public void onClick(View v) {
switch ((int) v.getTag()) {
case LEFT_BUTTON:
mToptitleClickListener.leftClick();
break;
case RIGHT_BUTTON:
mToptitleClickListener.rightClick();
break;
default:
break;
}
}
xmlns:toptitle="http://schemas.android.com/apk/res-auto"
topTitle= (TopTitle) findViewById(R.id.top_title);
topTitle.setOnToptitleClickListener(new TopTitle.ToptitleClickListener() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this,"这是左边",Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this,"这是右边",Toast.LENGTH_SHORT).show();
}
});
此种方式较为繁琐复杂,只是一种思路,不推荐使用,更推荐下面几种方式。
此种方式较为简单,实现与第一种类似,但比第一种更为简单
public class TopTitleViewextends LinearLayout{
private LinearLayout mTopBack;
public TextView mTvBack;
public TextView mTvTitle;
public ImageView mIvRight;
public TextView mTvRight;
private Activity mActivity;
public TopTitleView(Context context) {
super(context);
}
public TopTitleView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.top_bar_view, this, true);
mTopBack=(LinearLayout)this.findViewById(R.id.top_back_btn);
mTvBack=(TextView)this.findViewById(R.id.top_back_tv);
mTvTitle=(TextView)this.findViewById(R.id.top_title);
mTvRight=(TextView)this.findViewById(R.id.top_right_tv);
mIvRight=(ImageView)this.findViewById(R.id.top_right_btn);
mTopBack.setOnClickListener(onClickListener);
mTvRight.setOnClickListener(onClickListener);
mIvRight.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.top_back_btn:
if(mActivity!=null)
mActivity.finish();
break;
case R.id.top_right_tv:
break;
case R.id.top_right_btn:
break;
}
}
};
public void setActivity(Activity activity) {
this.mActivity=activity;
}
public void setTitle(String title) {
mTvTitle.setText(title);
}
public void setRightText(String text) {
mTvRight.setText(text);
}
}
3.usage in xmls
4.usage in activities
mTopBar.setActivity(this);
mTopBar.setTitle("这是哪一页");
5.总结,个人比较喜欢这种方式,方便快捷,简单。
8-12
8-13