自定义控件
/** 标题栏, 可设置标题和左右图标*/
引用库
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class ActionBarTextView extends FrameLayout {
private TextView mTitleView;
private TextView mLeftActionButton;
private TextView mActionBarTitle;
private TextView rightActionButton;
private View actionBarLayout;
private ImageView rightActionImageView;
public ActionBarTextView(Context context) {
super(context);
}
public ActionBarTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ActionBarTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@SuppressLint("CutPasteId")
private void init() {
LayoutInflater.from(getContext()).inflate(
R.layout.action_bar_layout_text, this);
mTitleView = (TextView) findViewById(R.id.actionBarTitle);
mLeftActionButton = (TextView) findViewById(R.id.leftActionButton);
mActionBarTitle = (TextView) findViewById(R.id.actionBarTitle);
rightActionButton = (TextView) findViewById(R.id.rightActionButton);
rightActionImageView = (ImageView) findViewById(R.id.rightActionImageView);
actionBarLayout = findViewById(R.id.actionBarLayout);
try {
int color = Color.parseColor(CacheUtil.getAppConfig(getContext())
.getThemeColor());
actionBarLayout.setBackgroundColor(color);
} catch (Exception e) {
e.printStackTrace();
}
hideLeftActionButtonText();
}
public void setTitle(String text) {
mTitleView.setText(text);
}
public void setTitle(int text) {
mTitleView.setText(text);
}
public void setTitleTextColor(int resId) {
mTitleView.setTextColor(getResources().getColor(resId));
}
public void setLeftActionButton(int icon, OnLimitClickHelper listener) {
if (icon != R.mipmap.btn_back) {
hideLeftActionButtonText();
}
if (icon != 0) {
Drawable drawable = getResources().getDrawable(icon);
// / 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
mLeftActionButton.setCompoundDrawables(drawable, null, null, null);
} else {
Drawable drawable = getContext().getResources().getDrawable(
R.drawable.btn_goback_style);
mLeftActionButton.setBackgroundDrawable(drawable);
}
mLeftActionButton.setOnClickListener(listener);
}
public void setRightActionButton(int icon, OnLimitClickHelper listener) {
if (icon != 0) {
Drawable drawable = getResources().getDrawable(icon);
// / 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
rightActionButton.setCompoundDrawables(drawable, null, null, null);
} else {
Drawable drawable = getContext().getResources().getDrawable(
R.drawable.btn_goback_style);
rightActionButton.setBackgroundDrawable(drawable);
}
rightActionButton.setOnClickListener(listener);
}
/**
* 给title加点击事件
*
* @param listener
*/
public void setActionBarTitleClickListener(OnLimitClickHelper listener) {
mActionBarTitle.setOnClickListener(listener);
}
/**
* 隐藏左上角按钮的文字
*/
public void hideLeftActionButtonText() {
mLeftActionButton.setText("");
}
/**
* 隐藏右上角按钮的文字
*/
public void hideRightActionButtonText() {
rightActionButton.setText("");
}
/**
* 修改右上角按钮的文字
*/
public void setRightActionButtonText(String text) {
rightActionButton.setText(text);
}
/**
* 隐藏右上角按钮图片
*/
public void hideRightActionButtonImage() {
rightActionButton.setCompoundDrawables(null, null, null, null);
}
/**
* 隐藏左上角按钮
*/
public void hideLeftActionButton() {
mLeftActionButton.setVisibility(View.GONE);
}
/**
* 隐藏右上角按钮
*/
public void hideRightActionButton() {
rightActionButton.setVisibility(View.GONE);
}
public void setRightActionButton(String text, OnLimitClickHelper listener) {
rightActionButton.setCompoundDrawables(null, null, null, null);
rightActionButton.setText(text);
rightActionButton.setOnClickListener(listener);
}
public void setLeftActionButtonText(String text, OnLimitClickHelper listener) {
mLeftActionButton.setCompoundDrawables(null, null, null, null);
mLeftActionButton.setText(text);
mLeftActionButton.setOnClickListener(listener);
}
public void setrightActionImageViewVisible(int icon,
OnLimitClickHelper listener) {
rightActionImageView.setVisibility(View.VISIBLE);
if (icon != 0) {
Drawable drawable = getResources().getDrawable(icon);
// / 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
rightActionImageView.setBackgroundDrawable(drawable);
}
rightActionImageView.setOnClickListener(listener);
}
}
网友评论