自定义ToolBar 头部
程序员文章站
2022-07-12 13:58:16
...
1,studio 5.0 新特性时代,ToolBar代替ActionBar ,ToolBar的基本属性
2,修改主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
3,填充 toolbar 里面的布局文件:
<?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">
<ImageView
android:id="@id/back"
android:layout_centerVertical="true"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<EditText
android:id="@+id/toolbar_searchview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:textSize="14sp"
android:gravity="center"
android:padding="3dp"
android:background="@drawable/toolbar_edittext_bg_shape"
android:layout_centerVertical="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:drawableLeft="@mipmap/icon_search"
android:visibility="gone"
/>
<TextView
android:id="@+id/toolbar_title"
android:text="haha"
android:textSize="20sp"
android:textColor="@color/white"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<!--style="@android:style/Widget.Material.Toolbar.Button.Navigation"-->
<Button
android:id="@+id/toolbar_rightButton"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="@color/white"
style="@android:style/Widget.Material.Toolbar.Button.Navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"/>
</RelativeLayout>
4,Java中代码部分
public class MyToolBar extends Toolbar {
private LayoutInflater mLayoutInflater;
private View mView;
private EditText mToolbar_searchview;
private Button mToolbar_rightButton;
private TextView mToolbar_title;
private ImageView mBack;
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);
if(attrs!=null){
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
R.styleable.MyToolBar, defStyleAttr, 0);
// 从属性中获取 rightButtonIcon
Drawable rightIcon = a.getDrawable(R.styleable.MyToolBar_rightButtonIcon);
if(rightIcon!=null){
setRightBtnIcon(rightIcon);
}
// 从属性中获取 rightButtonTitle
String rightTitle = a.getString(R.styleable.MyToolBar_rightButtonTitle);
if(rightTitle!=null){
setRightBtnTitle(rightTitle);
}
// 从属性中获取 backIcon
Drawable backIcon = a.getDrawable(R.styleable.MyToolBar_backIcon);
if(backIcon!=null){
setBackBtnIcon(backIcon);
}
boolean isShowSearchView = a.getBoolean(R.styleable.MyToolBar_isShowSearchView, false);
if(isShowSearchView){
showSearchView();
hideTitleView();
}
// 资源回收
a.recycle();
}
}
private void initView() {
if(mView==null){
mLayoutInflater=LayoutInflater.from(getContext());
mView = mLayoutInflater.inflate(R.layout.layout_toolbar, null);
mToolbar_searchview = (EditText) mView.findViewById(R.id.toolbar_searchview);
mToolbar_title = (TextView) mView.findViewById(R.id.toolbar_title);
mToolbar_rightButton = (Button) mView.findViewById(R.id.toolbar_rightButton);
mBack = (ImageView) mView.findViewById(R.id.back);
//然后使用LayoutParams把控件添加到子view中
LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
addView(mView,params);
}
}
@Override
public void setTitle(@StringRes int resId) {
setTitle(getContext().getText(resId));
}
// 设置字体 重写父类的 settitle 方法
@Override
public void setTitle(CharSequence title) {
initView();
if(mToolbar_title!=null){
mToolbar_title.setText(title);
showTitleView();
}
}
// 显示 title
private void showTitleView() {
if(mToolbar_title!=null){
mToolbar_title.setVisibility(View.VISIBLE);
}
}
// 隐藏title
private void hideTitleView() {
if(mToolbar_title!=null){
mToolbar_title.setVisibility(View.GONE);
}
}
// 设置右侧ImageBtn 的图片
private void setRightBtnIcon(Drawable icon){
if(mToolbar_rightButton!=null){
mToolbar_rightButton.setBackgroundDrawable(icon);
mToolbar_rightButton.setVisibility(View.VISIBLE);
}
}
// 设置右侧 ImageBtn 的图片
// 设置右侧 ImageBtn 的图片
private void setRightBtnIcon(int icon){
setRightBtnIcon(getResources().getDrawable(icon));
}
// 设置右侧ImageBtn 的title
private void setRightBtnTitle(CharSequence title){
if(mToolbar_rightButton!=null){
mToolbar_rightButton.setText(title);
mToolbar_rightButton.setVisibility(View.VISIBLE);
}
}
// 设置右侧 ImageBtn 的title
private void setRightBtnTitle(int ResId){
setRightBtnTitle(getContext().getText(ResId));
}
// 设置左侧图片
private void setBackBtnIcon(Drawable icon){
if(mBack!=null){
mBack.setImageDrawable(icon);
mBack.setVisibility(View.VISIBLE);
}
}
// 设置左侧图片
private void setBackBtnIcon(int icon){
setRightBtnIcon(getResources().getDrawable(icon));
}
private void showSearchView() {
if(mToolbar_searchview!=null){
mToolbar_searchview.setVisibility(View.VISIBLE);
}
}
private void hideSearchView() {
if(mToolbar_searchview!=null){
mToolbar_searchview.setVisibility(View.GONE);
}
}
private void setRightBtnOnClick(OnClickListener listener){
mToolbar_rightButton.setOnClickListener(listener);
}
private void setBackBtnOnClick(OnClickListener listener){
mBack.setOnClickListener(listener);
}
}
5,attrs 中代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
resources>
<declare-styleable name="MyToolBar">
<!--<attr name="leftButtonIcon" format="reference"/>-->
<attr name="rightButtonIcon" format="reference"/>
<attr name="rightButtonTitle" format="string"/>
<attr name="backIcon" format="reference"/>
<attr name="isShowSearchView" format="boolean"/>
</declare-styleable>
</resources>
6,使用方法:
<com.vily.cartshop.ui.MyToolBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:isShowSearchView="true"
>