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顶部布局
* 一般是左边返回键,中间标题,右边图标
*
* @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 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="这是顶部标题的类容" />
<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="中间的标题文字" />
效果:

代码中设置相关点击事件:
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;
}
}
}
网友评论