自定义控件:实现加减功能
程序员文章站
2022-06-08 23:47:38
...
XML:
<?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>
attrs :
<declare-styleable name="NumberAddView">
<!--<attr name="leftButtonIcon" format="reference"/>-->
<attr name="value" format="integer"/>
<attr name="maxNum" format="integer"/>
<attr name="minNum" format="integer"/>
</declare-styleable>
Java代码:
package com.vily.cartshop.ui;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.vily.cartshop.R;
/**
* description :
* user : Administrator
* Created by Administrator on 2017\11\30 0030.
*/
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 的图片
public void setRightBtnIcon(Drawable icon){
if(mToolbar_rightButton!=null){
mToolbar_rightButton.setBackgroundDrawable(icon);
mToolbar_rightButton.setVisibility(View.VISIBLE);
}
}
// 设置右侧 ImageBtn 的图片
// 设置右侧 ImageBtn 的图片
public void setRightBtnIcon(int icon){
setRightBtnIcon(getResources().getDrawable(icon));
}
// 设置右侧ImageBtn 的title
public 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);
}
}
public void hideSearchView() {
if(mToolbar_searchview!=null){
mToolbar_searchview.setVisibility(View.GONE);
}
}
public void setRightBtnOnClick(OnClickListener listener){
mToolbar_rightButton.setOnClickListener(listener);
}
public void setBackBtnOnClick(OnClickListener listener){
mBack.setOnClickListener(listener);
}
public Button getRightBtn(){
return mToolbar_rightButton;
}
}