一 、自定义ToolBar前 ,先贴一下style ,相信大家一定和我一样感到既陌生又熟悉
二、接下来,开始自定义ToolBar,主要满足两个功能 1、居中标题、左右两个ImageButton 2、带searchView 的Toolbar 听起来很简单,主要是复习一下组合式的自定义控件
下面直接贴源码
2.1、
/**
* Created by 蔡宇奎 on 2017-3-17.
*/
public class MyToolBar extends Toolbar implements TextWatcher {
private EditText toolbar_editText;
private TextView toolbar_search;
private TextView toolbar_textView;
private ImageButton toolbar_imgBtn;
private ImageButton toolbar_LeftimgBtn;
private MyToolBarBtnListenter btnListenter;
private MyToolBarEditTextListener editTextListener;
public MyToolBar(Context context) {
this(context, null);
}
public MyToolBar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, android.support.v7.appcompat.R.attr.toolbarStyle);
}
public MyToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
// setContentInsetsRelative(100,100);
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
R.styleable.MyToolBar, defStyleAttr, 0);
Drawable drawableRight = a.getDrawable(R.styleable.MyToolBar_RightImgButtonIcon);
Drawable drawableLeft = a.getDrawable(R.styleable.MyToolBar_LeftImgButtonIcon);
boolean aBoolean = a.getBoolean(R.styleable.MyToolBar_isShowEditText, false);
String hint = a.getString(R.styleable.MyToolBar_editHint);
if(!TextUtils.isEmpty(hint)){
toolbar_editText.setHint(hint);
}
if (drawableRight != null) {
setRightImageBtnDrawable(drawableRight);
}
if (drawableLeft != null) {
setLeftImageBtnDrawable(drawableLeft);
}
/**
* 如果显示editText为true,则把editText 设为显示,TextView设为隐藏
*/
if (aBoolean) {
showEditText();
hintTextView();
} else {
hintEditText();
}
}
private void initView() {
View view = View.inflate(getContext(), R.layout.mytoolbar_layout, null);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
addView(view, params);
toolbar_editText = (EditText) this.findViewById(R.id.toolbar_editText);
toolbar_textView = (TextView) this.findViewById(R.id.toolbar_textView);
toolbar_imgBtn = (ImageButton) this.findViewById(R.id.toolbar_imgBtn);
toolbar_LeftimgBtn = (ImageButton) this.findViewById(R.id.toolbar_leftImgBtn);
toolbar_search = (TextView) this.findViewById(R.id.toolbar_search);
toolbar_editText.addTextChangedListener(this);
toolbar_imgBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != btnListenter) {
btnListenter.ImageRightBtnclick();
}
}
});
toolbar_LeftimgBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != btnListenter) {
btnListenter.ImageLeftBtnclick();
}
}
});
toolbar_search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != btnListenter) {
if(TextUtils.isEmpty(toolbar_editText.getText())){
Toast.makeText(getContext(),"输入为空",Toast.LENGTH_SHORT).show();
return;
}
btnListenter.searchBtnclick(toolbar_editText.getText().toString());
}
}
});
toolbar_editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Log.e("editText", "失去焦点");
// 失去焦点
toolbar_editText.setCursorVisible(false);
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(toolbar_editText.getWindowToken(), 0);
}else{
Log.e("editText", "获得焦点");
toolbar_editText.setCursorVisible(true);
}
}
});
}
public void showTextView() {
toolbar_textView.setVisibility(View.VISIBLE);
}
public void showEditText() {
toolbar_editText.setVisibility(View.VISIBLE);
}
public void showRightBtnIcon() {
toolbar_imgBtn.setVisibility(View.VISIBLE);
}
public void showLeftBtnIcon() {
toolbar_LeftimgBtn.setVisibility(View.VISIBLE);
}
public void showToolbar_search(){
toolbar_search.setVisibility(View.VISIBLE);
}
public void hintToolbar_search(){
toolbar_search.setVisibility(View.GONE);
}
public void hintTextView() {
toolbar_textView.setVisibility(View.GONE);
}
public void hintEditText() {
toolbar_editText.setVisibility(View.GONE);
}
public void hintRightBtnIcon() {
toolbar_imgBtn.setVisibility(View.GONE);
}
@Override
public void setTitle(@StringRes int resId) {
showTextView();
if (resId != 0) {
toolbar_textView.setText(resId);
}
}
@Override
public void setTitle(CharSequence title) {
initView();
showTextView();
if (title != null) {
toolbar_textView.setText(title);
}
}
public void setLeftImageBtnDrawable(Drawable resId) {
showLeftBtnIcon();
toolbar_LeftimgBtn.setImageDrawable(resId);
}
public void setLeftImageBtnDrawable(int resId) {
showLeftBtnIcon();
toolbar_LeftimgBtn.setImageResource(resId);
}
public void setRightImageBtnDrawable(Drawable resId) {
showRightBtnIcon();
toolbar_imgBtn.setImageDrawable(resId);
}
public void setRightImageBtnResource(int resId) {
showRightBtnIcon();
toolbar_imgBtn.setImageResource(resId);
}
public void setMyToolBarBtnListenter(MyToolBarBtnListenter btnListenter) {
this.btnListenter = btnListenter;
}
public void setMyToolBarEditTextListener(MyToolBarEditTextListener editTextListener) {
this.editTextListener = editTextListener;
}
/**
* Log.i(TAG, "输入文本之前的状态");
*
* @param s
* @param start
* @param count
* @param after
*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (null != editTextListener) {
editTextListener.beforeTextChanged(s, start, count, after);
}
}
/**
* Log.i(TAG, "输入文字中的状态,");
*
* @param s
* @param start
* @param before
* @param count
*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (null != editTextListener) {
editTextListener.onTextChanged(s, start, before, count);
}
}
/**
* Log.i(TAG, "输入文字后的状态");
*
* @param s
*/
@Override
public void afterTextChanged(Editable s) {
if(TextUtils.isEmpty(s)){
showRightBtnIcon();
hintToolbar_search();
}else {
hintRightBtnIcon();
showToolbar_search();
}
if (null != editTextListener) {
editTextListener.afterTextChanged(s);
}
}
public interface MyToolBarBtnListenter {
void ImageRightBtnclick();
void ImageLeftBtnclick();
void searchBtnclick(String content);
}
public interface MyToolBarEditTextListener {
void beforeTextChanged(CharSequence s, int start, int count, int after);
void onTextChanged(CharSequence s, int start, int before, int count);
void afterTextChanged(Editable s);
}
}
2.2自定义属性
2.3 组合空间布局