public class MyToolBar extends Toolbar {
private EditText mSearch_view;
private TextView mToolbar_title;
private Button mRight_button;
private LayoutInflater mInflater;
private View mView;
public MyToolBar(Context context) {
this(context, null);
}
public MyToolBar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
//设置边距
setContentInsetsRelative(10, 10);
//读取自定义的属性attrs文件
final TintTypedArray mTypeArray = TintTypedArray.obtainStyledAttributes(getContext(),
attrs,
R.styleable.MyToolBar,
defStyleAttr,0);
//获取自定义属性
final Drawable rightbutton = mTypeArray.getDrawable(R.styleable.MyToolBar_RightButton);
//设置右上角按钮图标
if (rightbutton != null) {
setRightButtonIcon(rightbutton);
}
//设置右上角按钮的文字
final CharSequence rightText = mTypeArray.getText(R.styleable.MyToolBar_rightButtonText);
if (rightText != null){
setRightButtonText(rightText);
}
//设置显示空件
boolean isShowSearchView = mTypeArray.getBoolean(R.styleable.MyToolBar_isShowSearchView, false);
if (isShowSearchView) {
showSearchView();
hideRightButtonView();
hideTitleView();
} else {
showTitleView();
showRightButtonView();
hideSearchView();
}
//回收设置属性
mTypeArray.recycle();
}
private void initView() {
if (mView == null) {
mInflater = LayoutInflater.from(getContext());
mView = mInflater.inflate(R.layout.toolbar, null);
mSearch_view = (EditText) mView.findViewById(R.id.search_edit);
mToolbar_title = (TextView) mView.findViewById(R.id.toolbar_title);
mRight_button = (Button) mView.findViewById(R.id.toolbar_right_button);
//设置toolbar在父容器的布局
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
//加载视图到父视图
addView(mView,lp);
}
}
//设置按钮监听事件
public void setRightButtonClickListener(OnClickListener listener) {
mRight_button.setOnClickListener(listener);
}
//调用此方法传入资源ID,然后调用父类的setTitle设置标题
@Override
public void setTitle(@StringRes int resId) {
setTitle(getContext().getText(resId));
}
@Override
public void setTitle(CharSequence title) {
initView();
if (mToolbar_title != null) {
mToolbar_title.setText(title);
showTitleView();
}
}
public void showTitleView() {
if (mToolbar_title != null) {
mToolbar_title.setVisibility(VISIBLE);
}
}
public void hideTitleView() {
if (mToolbar_title != null) {
mToolbar_title.setVisibility(GONE);
}
}
public void showRightButtonView() {
if (mRight_button != null) {
mRight_button.setVisibility(VISIBLE);
}
}
public void hideRightButtonView() {
if (mRight_button != null) {
mRight_button.setVisibility(GONE);
}
}
public void showSearchView() {
if (mSearch_view != null) {
mSearch_view.setVisibility(VISIBLE);
}
}
public void hideSearchView() {
if (mSearch_view != null) {
mSearch_view.setVisibility(GONE);
}
}
//设置右上角图标的函数
public void setRightButtonIcon(int icon){
setRightButtonIcon(ContextCompat.getDrawable(getContext(),icon));
}
public void setRightButtonIcon(Drawable rightButtonIcon) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mRight_button.setBackground(rightButtonIcon);
}
mRight_button.setVisibility(VISIBLE);
}
//设置右上角按钮文字
public void setRightButtonText(CharSequence text){
mRight_button.setText(text);
mRight_button.setVisibility(VISIBLE);
}
public void setRightButtonText(int id){
setRightButtonText(getResources().getString(id));
}
//获取按钮实例
public Button getRightButton(){
return this.mRight_button;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--搜索框-->
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/icon_search"
android:layout_margin="5dp"
android:hint="@string/seach_edit_hint"
android:gravity="center"
style="@style/search_view"
android:visibility="gone"
/>
<!--居中的Title-->
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:text="主页"
android:visibility="gone"
android:textColor="@color/white"
android:textSize="@dimen/toolbar_title"
/>
<!--右上角的按钮-->
<Button
android:id="@+id/toolbar_right_button"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="13dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyToolBar">
<attr name="RightButton" format="reference"/>
<attr name="isShowSearchView" format="boolean"/>
<attr name="rightButtonText" format="string"/>
</declare-styleable>
</resources>
<news.huoren.com.myshop.widget.MyToolBar
android:id="@+id/tab_home_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:isShowSearchView="true"
/>