1.首先在res——values下新建一个atts.xml文件,代码如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="titleText" format="string"></attr>
<attr name="titleTextSize" format="dimension"></attr>
<attr name="titleTextColor" format="color"></attr>
<attr name="leftTextColor" format="color"></attr>
<attr name="leftBackground" format="reference|color"></attr>
<attr name="leftText" format="string"></attr>
<attr name="rightTextColor" format="color"></attr>
<attr name="rightBackground" format="reference|color"></attr>
<attr name="rightText" format="string"></attr>
</declare-styleable>
</resources>
</pre><p><span style="font-size:18px;">2.自定义Topbar类,继承Relativelayout,代码如下</span></p><p><span style="font-size:18px;"></span><pre name="code" class="java">package com.weixi.topbar;
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.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by 风华国乐 on 2016/8/2.
*/
public class Topbar extends RelativeLayout {
//定义标题导航栏左右两边的按钮和中间的文字
private Button leftButton, rightButton;
private TextView tvTitle;
//左边Buttton的背景,颜色,名字
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
//右边Button
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
//中间title的属性(颜色,文字,文字大小)
private float titleTextSize;
private String titlt;
private int titltColor;
//将控件放入layout中还需要layoutparams
private LayoutParams leftParams, rightParams, titleParams;
//(点击事件)为回调机制定义一个接口
public interface topbadClickListener{
public void leftClick();
public void rightClick();
}
//设置一个接口对象
private topbadClickListener listener;
//设置一个调用接口的方法
public void setOnTopbarClickListener(topbadClickListener listener){
this.listener=listener;
}
public Topbar(final Context context, AttributeSet attrs) {
super(context, attrs);
//找到定义的topbar
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
//左边
leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
leftText = ta.getString(R.styleable.Topbar_leftText);
//右边
rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
rightText = ta.getString(R.styleable.Topbar_rightText);
//中间title
titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
titlt = ta.getString(R.styleable.Topbar_titleText);
titltColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
ta.recycle();
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
//将获得的属性赋值给控件
leftButton.setText(leftText);
leftButton.setBackground(leftBackground);
leftButton.setTextColor(leftTextColor);
rightButton.setText(rightText);
rightButton.setBackground(rightBackground);
rightButton.setTextColor(rightTextColor);
tvTitle.setText(titlt);
tvTitle.setTextColor(titltColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);
//给ViewGroup设置背景颜色
setBackgroundColor(0XFFF59563);
//设置控件的位置
//定义左边按钮的宽高
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//定义左对齐
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
//加入到ViewGroup中
addView(leftButton,leftParams);
//定义右边按钮的宽高
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//定义右对齐
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
//加入到ViewGroup中
addView(rightButton,rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT );
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle,titleParams);
//为按钮增加点击事件
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
//原来是直接在里面设置点击事件
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
}
}
3.MainActivity中的设置
package com.weixi.topbar;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Topbar topbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topbar = (Topbar) findViewById(R.id.topbar);
topbar.setOnTopbarClickListener(new Topbar.topbadClickListener() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this, "返回", Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this, "更多", Toast.LENGTH_SHORT).show();
}
});
}
}