自定义toolbar

toolbar运行效果

这里写图片描述

public class My extends Toolbar {

    TextView textLeft;//左边按钮
    TextView textCenter;//中间按钮
    TextView textRight;//右边按钮
    public My(Context context) {
        super(context,null);
    }

    public My(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs,0);
    }

    public My(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        textLeft=(TextView) this.findViewById(R.id.txt_left);
        textCenter=(TextView) this.findViewById(R.id.txt_center);
        textRight=(TextView) this.findViewById(R.id.txt_right);
    }

    /**
     * 设置中间文本框内容
     * @param text  字符串对像
     */
    public void setCenterTitle(String text){
        this.setTitle("");

        textCenter.setVisibility(VISIBLE);
        textCenter.setText(text);
    }
    /*
     实质中间文字的颜色
     */
    public void setCenterColor(int color){
        textCenter.setTextColor(color);
    }
    /*
    设置左边文本框文字信息
     */
    public void  setLeftText(String text){
        this.setTitle("");
        textLeft.setVisibility(VISIBLE);
        textLeft.setText(text);
    }
    /*
    实质左侧文字的颜色
    */
    public void setLeftColor(int color){
        textCenter.setTextColor(color);
    }

    /**
     * 获取左侧文本框图片资源
     * @param res
     */
    public void setLeftDrawable(int res){
        Drawable drawable= ContextCompat.getDrawable(getContext(),res);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        textLeft.setCompoundDrawables(drawable,null,null,null);

    }

    /**
     * 左侧文本框绑定监听事件
     * @param onClickListener
     */
    public void setLeftListener(OnClickListener onClickListener){
        textLeft.setOnClickListener(onClickListener);
    }

    /**
     * 设置右侧文本框文字信息
     * @param title
     */
    public void setRightTitle(String title){
        this.setTitle("");
        textRight.setVisibility(VISIBLE);
        textRight.setText(title);
    }

    /**
     * 设置右侧文本框文字颜色
     * @param color  颜色
     */
    public void setRightColor(int color){
        textRight.setTextColor(color);
    }

    /***
     * 设置右侧文本框图片资源
     * @param res  资源名
     */
    public  void setRightDrawable(int res){
        Drawable drawable=ContextCompat.getDrawable(getContext(),res);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        textRight.setCompoundDrawables(drawable,null,null,null);
    }
    public void setRightListener(OnClickListener onClickListener){
        textRight.setOnClickListener(onClickListener);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="wz.com.mytoolbar.MainActivity">

    <wz.com.mytoolbar.My
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:id="@+id/my">
        <!--android:lines="1" 行数-->
        <TextView
            android:textSize="20sp"
            android:id="@+id/txt_left"
            android:text="返回"
            android:textColor="@color/textToolBarColor"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:visibility="visible"
            android:lines="1"
            android:drawableLeft="@drawable/call_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:ignore="HardcodedText,RtlHardcoded" />
        <TextView
            android:textSize="20sp"
            android:id="@+id/txt_center"
            android:text="标题"
            android:textColor="@color/textToolBarColor"
            android:layout_gravity="center"
            android:lines="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:ignore="HardcodedText" />
        <TextView
            android:drawableRight="@drawable/more_pic"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:textSize="16sp"
            android:visibility="visible"
            android:id="@+id/txt_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:ignore="RtlHardcoded" />
    </wz.com.mytoolbar.My>
</RelativeLayout>

这里写图片描述


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        My my=(My) this.findViewById(R.id.my);

        //实例化my里的组件
        my.onFinishInflate();

        //实现返回按钮点击
        my.setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"你点击了返回按钮",Toast.LENGTH_SHORT).show();
            }
        });
        //实现更多按钮的点击
        my.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"你点击了更多按钮",Toast.LENGTH_SHORT).show();

            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/fruit_fruit/article/details/78829218