https://github.com/helloworld107/CainiaoMarket项目地址
由于在多个活动中都会用到toolbar,重复写代码布局显然会浪费更多的时间和精力,同时为了toolbar的灵活性和扩展性,所以实际开发会采用自定义控件的toolba
自定义toolbar的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
app:android="http://schemas.android.com/apk/res-auto" 自定义属性要自定义命名空间,名字随意,引用要用写好的名字,比如现在的app
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/toolbar_searchview"
style="@style/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:drawableLeft="@mipmap/icon_search"
android:gravity="center"
android:textSize="14sp"
android:hint="请输入搜索内容"
android:visibility="visible"
/>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_alignParentLeft="true"
android:gravity="center"
android:textColor="@color/white"
android:textSize="14sp"
android:visibility="gone"
android:text="购物车"
/>
<Button
android:id="@+id/toolbar_rightButton"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@color/transparent"
android:layout_marginRight="10dp"
android:minWidth="56dp"
android:text="分享"
android:textColor="@color/white"
android:visibility="gone"
/>
</RelativeLayout>
attrs.xml文件设置自定义属性
根据需要更改控件的显示,内容
<resources>
<declare-styleable name="MyToolBar">
<attr name="rightButtonIcon" format="reference"/>
<attr name="isShowSearchView" format="boolean"/>
<attr name="rightButtonText" format="string"/>
</declare-styleable>
</resources>
public class MyToolbar extends Toolbar {
EditText mToolbarSearchview;
TextView mToolbarTitle;
Button mToolbarRightButton;
private View mView;
private TintTypedArray mTintTypedArray;
public MyToolbar(Context context) {
this(context, null);
}
public MyToolbar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();//通过addView便可以添加自己的布局
//可以理解为padding,toolbar的内边距
setContentInsetsRelative(10, 10);
//封装了TypedArray,功能更强大
//拿到toolbar自定义的属性,主要都是button的
if (attrs != null) {
mTintTypedArray = TintTypedArray.obtainStyledAttributes(getContext(), attrs, R.styleable.MyToolBar, defStyleAttr, 0);
//拿到自定义属性
Drawable buttonDrawable = mTintTypedArray.getDrawable(R.styleable.MyToolBar_rightButtonIcon);
boolean isShowSearchView = mTintTypedArray.getBoolean(R.styleable.MyToolBar_isShowSearchView,false);
String buttonText = mTintTypedArray.getString(R.styleable.MyToolBar_rightButtonText);
//设置按钮图标
if (buttonDrawable != null) {
setRightButtonDrawable(buttonDrawable);
}
//设置按钮内容
setRightButtonText(buttonText);
//根据需要释放显示编辑框
if (isShowSearchView) {
showSerachView();
hideTextView();
}else {
hideSerachView();
showTextView();
}
mTintTypedArray.recycle();//刷新
}
}
//自定义自己的view
private void initView() {
if (mView == null) {
mView = View.inflate(getContext(), R.layout.toolbar, null);
mToolbarTitle = (TextView) mView.findViewById(R.id.toolbar_title);
mToolbarSearchview = (EditText) mView.findViewById(R.id.toolbar_searchview);
mToolbarRightButton = (Button) mView.findViewById(R.id.toolbar_rightButton);
}
//显示视图,和形状,默认不要焦点
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
addView(mView, params);//这样就可以显示视图了
}
public void hideTextView() {
if (mToolbarTitle != null) {
mToolbarTitle.setVisibility(GONE);
}
}
public void showTextView() {
if (mToolbarTitle != null) {
mToolbarTitle.setVisibility(VISIBLE);
}
}
public void showSerachView() {
if (mToolbarSearchview != null) {
mToolbarSearchview.setVisibility(VISIBLE);
}
}
public void hideSerachView() {
if (mToolbarSearchview != null) {
mToolbarSearchview.setVisibility(GONE);
}
}
public EditText getToolbarSearchview() {
return mToolbarSearchview;
}
public void setRightButtonText(String buttonText) {
mToolbarRightButton.setVisibility(VISIBLE);
if (mToolbarRightButton != null) {
mToolbarRightButton.setText(buttonText);
}
}
public void setTextTitle(String buttonText) {
mToolbarTitle.setVisibility(VISIBLE);
if (mToolbarTitle != null) {
mToolbarTitle.setText(buttonText);
}
}
public Button getRightButton(){
return this.mToolbarRightButton;
}
public Button getToolbarRightButton() {
return mToolbarRightButton;
}
public void setRightButtonDrawable(Drawable drawable) {
if (mToolbarRightButton != null) {
mToolbarRightButton.setVisibility(VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mToolbarRightButton.setBackground(drawable);
} else {
mToolbarRightButton.setBackgroundDrawable(drawable);
}
}
}
//按钮监听事件
public void setButtonOnClickListener(OnClickListener listener){
if (mToolbarRightButton != null) {
mToolbarRightButton.setOnClickListener(listener);
}
}
}
这样我们就可以根据实际的情况控制toolbar啦