CommonTopView常见的顶部view

package com.example.zd.baselibrary.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zd.baselibrary.R;

/**
 * Package: com.example.zd.baselibrary.view
 * 

* describe: 常见的activity顶部布局 * 一般是左边返回键,中间标题,右边图标 * * @author zhangdong on 2019/5/6 0006. * @version 1.0 * @see . * @since 1.0 */ public class CommonTopView extends FrameLayout { public static final String TYPE_LEFT = "typeOfLeftView"; public static final String TYPE_RIGHT = "typeOfRightView"; private ImageView imgLeft; private ImageView imgRight; private TextView tvTitle; private Drawable leftIcon; private Drawable rightIcon; private String topTitle; private int textColor; private float textSize; private TopViewClickListener viewClickListener; public void setViewClickListener(TopViewClickListener viewClickListener) { this.viewClickListener = viewClickListener; } public CommonTopView(@NonNull Context context) { this(context, null); } public CommonTopView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public CommonTopView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTopView); leftIcon = typedArray.getDrawable(R.styleable.CommonTopView_leftIcon); rightIcon = typedArray.getDrawable(R.styleable.CommonTopView_rightIcon); topTitle = typedArray.getString(R.styleable.CommonTopView_topTitle); textColor = typedArray.getColor(R.styleable.CommonTopView_android_textColor, Color.BLACK); textSize = typedArray.getDimensionPixelSize(R.styleable.CommonTopView_android_textSize, 18); typedArray.recycle(); updateView(); } private void initView() { View inflate = LayoutInflater.from(getContext()) .inflate(R.layout.layout_common_top_view, this, true); imgLeft = ((ImageView) inflate.findViewById(R.id.img_left)); tvTitle = ((TextView) inflate.findViewById(R.id.tv_title)); imgRight = ((ImageView) inflate.findViewById(R.id.img_right)); imgLeft.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (null != viewClickListener) viewClickListener.viewClick(TYPE_LEFT, view); } }); imgRight.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (null != viewClickListener) viewClickListener.viewClick(TYPE_RIGHT, view); } }); } private void updateView() { if (null != leftIcon) imgLeft.setImageDrawable(leftIcon); if (null != rightIcon) imgRight.setImageDrawable(rightIcon); if (null != tvTitle) { tvTitle.setText(topTitle); tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); tvTitle.setTextColor(textColor); } } /** * 设置左边的图标 * * @param leftIcon 左边图标的资源id */ @SuppressLint("ResourceType") public void setLeftIcon(@IdRes int leftIcon) { if (0 == leftIcon) return; if (null != imgLeft) imgLeft.setImageResource(leftIcon); } /** * 设置右边的图标 * * @param rightIcon 右边的图标资源id */ @SuppressLint("ResourceType") public void setRightIcon(@IdRes int rightIcon) { if (0 == rightIcon) return; if (null != imgRight) imgRight.setImageResource(rightIcon); } /** * 设置中间的文字显示 * * @param topTitle 文字类容 */ public void setTopTitle(String topTitle) { this.topTitle = topTitle; if (null != topTitle) tvTitle.setText(topTitle); } /** * 设置文字颜色 * * @param textColor . */ public void setTextColor(int textColor) { this.textColor = textColor; if (null != tvTitle) tvTitle.setTextColor(textColor); } /** * 设置文字大小 * * @param textSize . */ public void setTextSize(float textSize) { this.textSize = textSize; if (null != tvTitle) tvTitle.setTextSize(textSize); } /** * 左边的图标是否可见 * * @param visible 是否可见 */ public void setImgLeftVisible(boolean visible) { if (null != imgLeft) imgLeft.setVisibility(visible ? VISIBLE : INVISIBLE); } /** * 右边的图标是否可见 * * @param visible 是否可见 */ public void setImgRightVisible(boolean visible) { if (null != imgRight) imgRight.setVisibility(visible ? VISIBLE : INVISIBLE); } public interface TopViewClickListener { void viewClick(String type, View view); } }

自定义属性




    

        
        
        
        
        

    



xml布局:




    

    

    



使用:

 

效果:


CommonTopView常见的顶部view_第1张图片
image.png

代码中设置相关点击事件:

public class MainActivity extends AppCompatActivity implements CommonTopView.TopViewClickListener {

    private CommonTopView topView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        topView = (CommonTopView) findViewById(R.id.top_view);
        topView.setViewClickListener(this);

    }

    @Override
    public void viewClick(String type, View view) {
        switch (type) {
            case TYPE_LEFT:
                Toast.makeText(this, ">>> left ", Toast.LENGTH_SHORT).show();
                break;
            case TYPE_RIGHT:
                Toast.makeText(this, ">>> right ", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

你可能感兴趣的:(CommonTopView常见的顶部view)