自定义ToolBar

Android官方提供的ToolBar虽然已经很强大,但是它的样式仍然有一些单调,现在自定义ToolBar来为它添加更多的新颖样式。比如SearchTextText可自由切换的ToolBar

实现步骤:

①自定义布局文件toolBar.xml,包括EditViewTextView和一个Button

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/toolbar_searchview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:drawableLeft="@mipmap/icon_search"
        style="@style/search_view"
        android:hint="请输入搜索内容"

        android:visibility="gone"
        />

    <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:gravity="center"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:visibility="gone"
        />

    <Button
        android:id="@+id/toolbar_rightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textColor="@color/white"
        android:visibility="gone"
        style="@android:style/Widget.Material.Toolbar.Button.Navigation"
        />

</RelativeLayout>

 

②自定义属性文件 attr.xml

<resources>
    <declare-styleable name="MyToolBar">
        <attr name="rightButtonIcon" format="reference"/>
        <attr name="isShowSearchView" format="boolean"/>
        <attr name="rightButtonText" format="string"/>
    </declare-styleable>
</resources>

 

③自定义ToolBar-----MyToolBar,继承ToolBar

public class MyToolBar extends Toolbar {

}

MyToolBar的构造函数

public CNiaoToolBar(Context contextAttributeSet attrs, int defStyleAttr) {
    super(contextattrsdefStyleAttr);
    initView();
    setContentInsetsRelative(10,10);

    if(attrs !=null) {
        final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext()attrs,
                R.styleable.MyToolBardefStyleAttr0);
        final Drawable rightIcon = a.getDrawable(R.styleable.MyToolBar_rightButtonIcon);
        if (rightIcon != null) {
            setRightButtonIcon(rightIcon);
        }
        boolean isShowSearchView = a.getBoolean(R.styleable.MyToolBar_isShowSearchView,false);
        if(isShowSearchView){
            showSearchView();
            hideTitleView();
        }
        CharSequence rightButtonText = a.getText(R.styleable.MyToolBar_rightButtonText);
        if(rightButtonText !=null){
            setRightButtonText(rightButtonText);
        }
        a.recycle();
    }
}

 

即首先初始化布局文件中的控件initView():

private void initView() {
    if(mView == null) {
        mInflater = LayoutInflater.from(getContext());
        mView mInflater.inflate(R.layout.toolbar, null);

        mTextTitle = (TextView) mView.findViewById(R.id.toolbar_title);
        mSearchView = (EditText) mView.findViewById(R.id.toolbar_searchview);
        mRightButton = (Button) mView.findViewById(R.id.toolbar_rightButton);

        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENTViewGroup.LayoutParams.WRAP_CONTENTGravity.CENTER_HORIZONTAL);

        addView(mViewlp);
    }
}

 

然后解析自定义属性

if(attrs !=null) {
    final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext()attrs,
            R.styleable.CNiaoToolBardefStyleAttr0);
    final Drawable rightIcon = a.getDrawable(R.styleable.CNiaoToolBar_rightButtonIcon);
    if (rightIcon != null) {
        //setNavigationIcon(navIcon);
        setRightButtonIcon(rightIcon);
    }
    boolean isShowSearchView = a.getBoolean(R.styleable.CNiaoToolBar_isShowSearchView,false);
    if(isShowSearchView){
        showSearchView();
        hideTitleView();
    }
    CharSequence rightButtonText = a.getText(R.styleable.CNiaoToolBar_rightButtonText);
    if(rightButtonText !=null){
        setRightButtonText(rightButtonText);
    }
    a.recycle();
}

现在就算定义好了一个自定义ToolBar

⑤在项目中使用MyToolBar时,完全与ToolBar的使用方法相同,

加入布局文件,获取MyToolBar,添加监听器,添加Settings选项

⑥显示Text文本

<com.example.administrator.shopdemo.widget.CNiaoToolBar
    android:id="@id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:rightButtonText="编辑"
    app:title="购物车" />

 

显示SearchView

<com.example.administrator.shopdemo.widget.CNiaoToolBar
    android:id="@+id/toolbar_home"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="商品详情"
    app:isShowSearchView="true"
    app:navigationIcon="@drawable/icon_back_32px"/>

 

private void initToolBar(View view){
    mToolBar = (CNiaoToolBar) view.findViewById(R.id.toolbar_home);
    mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getContext()"back"Toast.LENGTH_LONG).show();
        }
    });

    mToolBar.inflateMenu(R.menu.menu_main);
    mToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            if(id == R.id.action_settings){
                Toast.makeText(getContext()"menu"Toast.LENGTH_LONG).show();


                return true;
            }
            return false;

        }
    });
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值