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
 * <p>
 * describe: 常見的activity頂部布局
 * 一般是左邊返回鍵,中間標(biāo)題,右邊圖標(biāo)
 *
 * @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);
        }

    }

    /**
     * 設(shè)置左邊的圖標(biāo)
     *
     * @param leftIcon 左邊圖標(biāo)的資源id
     */
    @SuppressLint("ResourceType")
    public void setLeftIcon(@IdRes int leftIcon) {
        if (0 == leftIcon)
            return;
        if (null != imgLeft)
            imgLeft.setImageResource(leftIcon);
    }

    /**
     * 設(shè)置右邊的圖標(biāo)
     *
     * @param rightIcon 右邊的圖標(biāo)資源id
     */
    @SuppressLint("ResourceType")
    public void setRightIcon(@IdRes int rightIcon) {
        if (0 == rightIcon)
            return;
        if (null != imgRight)
            imgRight.setImageResource(rightIcon);
    }

    /**
     * 設(shè)置中間的文字顯示
     *
     * @param topTitle 文字類容
     */
    public void setTopTitle(String topTitle) {
        this.topTitle = topTitle;
        if (null != topTitle)
            tvTitle.setText(topTitle);
    }

    /**
     * 設(shè)置文字顏色
     *
     * @param textColor .
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
        if (null != tvTitle)
            tvTitle.setTextColor(textColor);
    }

    /**
     * 設(shè)置文字大小
     *
     * @param textSize .
     */
    public void setTextSize(float textSize) {
        this.textSize = textSize;
        if (null != tvTitle)
            tvTitle.setTextSize(textSize);
    }

    /**
     * 左邊的圖標(biāo)是否可見
     *
     * @param visible 是否可見
     */
    public void setImgLeftVisible(boolean visible) {
        if (null != imgLeft)
            imgLeft.setVisibility(visible ? VISIBLE : INVISIBLE);
    }

    /**
     * 右邊的圖標(biāo)是否可見
     *
     * @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 version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="CommonTopView">

        <attr name="leftIcon" format="reference" />
        <attr name="rightIcon" format="reference" />
        <attr name="topTitle" format="reference|string" />
        <attr name="android:textSize" />
        <attr name="android:textColor" />

    </declare-styleable>

</resources>

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="10dp"
        android:scaleType="centerInside"
        android:src="@drawable/icon_back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="1"
        android:maxLines="1"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/img_left"
        app:layout_constraintRight_toLeftOf="@+id/img_right"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="這是頂部標(biāo)題的類容" />

    <ImageView
        android:id="@+id/img_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="10dp"
        android:scaleType="centerInside"
        android:src="@drawable/icon_search"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

使用:

 <com.example.zd.baselibrary.view.CommonTopView
        android:id="@+id/top_view"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:textColor="@color/colorAccent"
        android:textSize="18sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:topTitle="中間的標(biāo)題文字" />

效果:


image.png

代碼中設(shè)置相關(guān)點擊事件:

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;
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,815評論 1 45
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,655評論 1 32
  • 翻譯自“Collection View Programming Guide for iOS” 0 關(guān)于iOS集合視...
    lakerszhy閱讀 4,073評論 1 22
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,150評論 1 92
  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,329評論 0 17

友情鏈接更多精彩內(nèi)容