public class MainActivity extends AppCompatActivity {
private MyViewGroup mygroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mygroup = (MyViewGroup) findViewById(R.id.mygroup);
mygroup.setOnTopbarClickListener(new MyViewGroup.topbarClickListener() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this,"点击了返回的按钮",Toast.LENGTH_LONG).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this,"点击了更多的按钮",Toast.LENGTH_LONG).show();
}
});
}
}
public class MyViewGroup extends RelativeLayout {
//组合控件属性
private String title;
private float titleTextSize;
private int titleTextColor;
private String leftText;
private Drawable leftBackground;
private int leftTextColor;
private String rightText;
private Drawable rightBackground;
private int rightTextColor;
private Button leftButton;
private Button rightButton;
private TextView titleView;
// 布局属性,用来控制组件元素在ViewGroup中的位置
private LayoutParams leftParams, titlepParams, rightParams;
// 映射传入的接口对象
private topbarClickListener mListener;
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.YELLOW);
//将在attrs.xml中定义的declare-styleable的所有属性存储到TypedArray中
//这里引用的styleable的TopBar,就是在xml中通过<declare-styleable name="TopBar">指定的name
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
//从TypedArray中取出对应的值来为要设置的属性赋值
title = ta.getString(R.styleable.TopBar_title);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,10);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
leftText = ta.getString(R.styleable.TopBar_leftText);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
rightText = ta.getString(R.styleable.TopBar_rightText);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
leftButton = new Button(context);
rightButton = new Button(context);
titleView = new TextView(context);
//为创建的组件元素赋值
//值来源于引用的xml文件中对应的值
leftButton.setText(leftText);
leftButton.setBackground(leftBackground);
leftButton.setTextColor(leftTextColor);
rightButton.setText(rightText);
rightButton.setBackground(rightBackground);
rightButton.setTextColor(rightTextColor);
titleView.setText(title);
titleView.setTextColor(titleTextColor);
titleView.setTextSize(titleTextSize);
titleView.setGravity(Gravity.CENTER);
//为组件元素这只相应的布局元素
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
//添加到ViewGroup
addView(leftButton,leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
//添加到ViewGroup
addView(rightButton,rightParams);
titlepParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titlepParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
//添加到ViewGroup
addView(titleView,titlepParams);
// 按钮的点击事件,不需要具体的实现,
// 只需调用接口的方法,回调的时候,会有具体的实现
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.rightClick();
}
});
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.leftClick();
}
});
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// 暴露一个方法给调用者来注册接口回调
// 通过接口来获得回调者对接口方法的实现
public void setOnTopbarClickListener(topbarClickListener mListener) {
this.mListener = mListener;
}
/**
* 设置按钮的显示与否 通过id区分按钮,flag区分是否显示
*/
public void setButtonVisable(int id, boolean flag) {
if (flag) {
if (id == 0) {
leftButton.setVisibility(View.VISIBLE);
} else {
rightButton.setVisibility(View.VISIBLE);
}
} else {
if (id == 0) {
leftButton.setVisibility(View.GONE);
} else {
rightButton.setVisibility(View.GONE);
}
}
}
// 接口对象,实现回调机制,在回调方法中
// 通过映射的接口对象调用接口中的方法
// 而不用去考虑如何实现,具体的实现由调用者去创建
public interface topbarClickListener {
// 左按钮点击事件
void leftClick();
// 右按钮点击事件
void rightClick();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.bawei.zhoukaoone.MyViewGroup
android:id="@+id/mygroup"
android:layout_width="match_parent"
android:layout_height="60sp"
custom:leftBackground="#0909d6"
custom:leftText="返回"
custom:leftTextColor="#fcf8f8"
custom:rightBackground="#0909d6"
custom:rightText="更多"
custom:rightTextColor="#fdfbfb"
custom:title="自定义标题"
custom:titleTextColor="#0d0d0d"
custom:titleTextSize="10sp"
/>
</LinearLayout>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/title_bar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp" />
<Button
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
</merge>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="title" 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>