项目中很多地方都有用到标题栏 ,如果每个地方都重新写一个有点麻烦,就自定义了一个带标题栏的相对布局
package com.vrseen.vivohome.view;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.vrseen.vivohome.R;
/**
* 自定义带有title的布局
* 封装带有title的布局,回调出title的点击事件
*
* @author [yangfei.cao]
* @see [相关类/方法]
* @since [产品/模块版本]
* @deprecated (可选)
*/
public class MyRelativeLayout extends RelativeLayout implements View.OnClickListener {
public static int LEFT_BUTTON = 0;
public static int RIGHT_BUTTON = 1;
public static int BOTTOM_BUTTON = 3;
public Button leftButton;
public Button rightButton;
public Button bottomButton;
public TextView titleTextView;
public RelativeLayout layout;
private String title;
private String titleLeftText;
private String titleRightText;
private String titleBottomText;
private float titleTextSize;
private float titleRightTextSize;
private float titleLeftTextSize;
private float titleBottomTextSize;
private int titleRightTextColor;
private int titleTextColor;
private int titleLeftTextColor;
private int titleBottomTextColor;
private Drawable titleLeftBackground;
private Drawable titleRightBackground;
private Drawable titleBottomBackground;
private ClickLisenter listener;
public interface ClickLisenter {
void Click(int tag);
}
public MyRelativeLayout(Context context) {
super(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
initView();
}
private void initAttrs(AttributeSet attrs) {
TypedArray ta = this.getContext().obtainStyledAttributes(attrs,
R.styleable.MyRelativeLayout);
if (ta != null) {
title = ta.getString(R.styleable.MyRelativeLayout_mytitle);
titleTextSize = ta
.getDimension(R.styleable.MyRelativeLayout_titleTextSize, 16);
titleTextColor = ta
.getColor(R.styleable.MyRelativeLayout_mytitleTextColor, 0);
titleLeftText = ta.getString(R.styleable.MyRelativeLayout_titleLeftText);
titleLeftBackground = ta
.getDrawable(R.styleable.MyRelativeLayout_titleLeftBackground);
titleLeftTextColor = ta.getColor(
R.styleable.MyRelativeLayout_titleLeftTextColor, 0);
titleLeftTextSize = ta
.getDimension(R.styleable.MyRelativeLayout_titleTextSize, 16);
titleRightText = ta.getString(R.styleable.MyRelativeLayout_titleRightText);
titleRightBackground = ta
.getDrawable(R.styleable.MyRelativeLayout_titleRightBackground);
titleRightTextColor = ta.getColor(
R.styleable.MyRelativeLayout_titleRightTextColor, 0);
titleRightTextSize = ta
.getDimension(R.styleable.MyRelativeLayout_titleTextSize, 16);
titleBottomText = ta.getString(R.styleable.MyRelativeLayout_titleBottomText);
titleBottomBackground = ta
.getDrawable(R.styleable.MyRelativeLayout_titleBottomBackground);
titleBottomTextColor = ta.getColor(
R.styleable.MyRelativeLayout_titleBottomTextColor, 0);
titleBottomTextSize = ta
.getDimension(R.styleable.MyRelativeLayout_titleBottomTextSize, 16);
ta.recycle();
}
}
@TargetApi(Build.VERSION_CODES.M)
private void initView() {
layout = new RelativeLayout(getContext());
leftButton = new Button(getContext());
titleTextView = new TextView(getContext());
rightButton = new Button(getContext());
bottomButton = new Button(getContext());
leftButton.setTextColor(titleLeftTextColor);
leftButton.setBackgroundDrawable(titleLeftBackground);
leftButton.setText(titleLeftText);
leftButton.setTextSize(titleLeftTextSize);
rightButton.setTextColor(titleRightTextColor);
rightButton.setBackgroundDrawable(titleRightBackground);
rightButton.setText(titleRightText);
rightButton.setTextSize(titleRightTextSize);
titleTextView.setText(title);
titleTextView.setTextSize(titleTextSize);
titleTextView.setTextColor(titleTextColor);
bottomButton.setText(titleBottomText);
bottomButton.setTextColor(titleBottomTextColor);
bottomButton.setBackgroundDrawable(titleBottomBackground);
bottomButton.setTextSize(titleBottomTextSize);
int color = getResources().getColor(R.color.gray);
layout.setBackgroundColor(color);
bottomButton.setVisibility(GONE);
int dimension = (int) getResources().getDimension(R.dimen.title_bar_height);
int bottomDimen = (int) getResources().getDimension(R.dimen.bottom_dimen);
//底部按钮
LayoutParams mBottomLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
bottomDimen);
mBottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
addView(bottomButton, mBottomLayoutParams);
LayoutParams layoutParams = newLayoutParams(LayoutParams.MATCH_PARENT, dimension);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
addView(layout, layoutParams);
LayoutParams mLeftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
mLeftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
mLeftLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(leftButton, mLeftLayoutParams);
LayoutParams mCenterLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
mCenterLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(titleTextView, mCenterLayoutParams);
LayoutParams mRightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
mRightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mRightLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(rightButton, mRightLayoutParams);
}
@Override
public void onClick(View v) {
if (listener != null) {
if (v == leftButton) {
listener.Click(LEFT_BUTTON);
} else if (v == rightButton) {
listener.Click(RIGHT_BUTTON);
} else if (v==bottomButton) {
listener.Click(BOTTOM_BUTTON);
}
}
}
/**
* 设置点击事件
*
* @param listener 点击事件的接口回调
*/
public void setClickLisenter(ClickLisenter listener) {
leftButton.setOnClickListener(this);
rightButton.setOnClickListener(this);
bottomButton.setOnClickListener(this);
this.listener = listener;
}
}
<declare-styleable name="MyRelativeLayout">
<attr name="mytitle" format="string" />
<attr name="titleTextSize" format="dimension" />
<attr name="mytitleTextColor" format="color" />
<attr name="titleLeftText" format="string" />
<attr name="titleLeftBackground" format="color|reference" />
<attr name="titleLeftTextColor" format="color" />
<attr name="titleLeftTextSize" format="dimension" />
<attr name="titleRightText" format="string" />
<attr name="titleRightBackground" format="color|reference" />
<attr name="titleRightTextColor" format="color" />
<attr name="titleRightTextSize" format="dimension" />
<attr name="titleBottomText" format="string" />
<attr name="titleBottomBackground" format="color|reference" />
<attr name="titleBottomTextColor" format="color" />
<attr name="titleBottomTextSize" format="dimension" />
</declare-styleable>