项目中有时候会用到当button点击后会出现变化的情况。一般的背景跟点击事件有时候是直接可以在drawable里面定义的,但是有时候又会出现只更改部分的属性,所以自己定义了个button。效果很简单 点击后button的背景跟字体会发生颜色的更改。先定义一个layout。
<?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="match_parent"
android:background="@android:color/white" >
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="@dimen/theme_h4"
android:textColor="@color/theme_font_thin3"/>
<View
android:id="@android:id/background"
android:layout_width="match_parent"
android:layout_height="@dimen/dp2"
android:background="@android:color/white"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
布局文件写好了。接下来就是view里面的定义了;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class StationButton extends FrameLayout {
protected RelativeLayout mycontent;
protected TextView textView;
protected View progress;
private String text;
private int textColor = -1;
private int textActColor = -1;
private int bgColor = -1;
private int bgActColor = -1;
private int viewColor = -1;
private int viewActColor = -1;
public StationButton(Context context) {
super(context);
this.initView();
}
public StationButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.station_top_nav);
this.text = ta.getString(R.styleable.station_top_nav_button_text);
this.textColor = ta.getResourceId(R.styleable.station_top_nav_button_text_color, -1);
this.textActColor = ta.getResourceId(R.styleable.station_top_nav_button_text_act_color, -1);
this.bgColor = ta.getResourceId(R.styleable.station_top_nav_button_bg_color, -1);
this.bgActColor = ta.getResourceId(R.styleable.station_top_nav_button_bg_act_color, -1);
this.viewColor = ta.getResourceId(R.styleable.station_top_nav_button_view_color, -1);
this.viewActColor = ta.getResourceId(R.styleable.station_top_nav_button_view_act_color, -1);
ta.recycle();
this.initView();
}
private void initView(){
this.mycontent = (RelativeLayout) LayoutInflater.from(this.getContext()).inflate(
R.layout.station_topbar_button_item, null);
this.textView = (TextView) this.mycontent.findViewById(android.R.id.text1);
this.textView.setPadding(0, 0, 0, DisplayUtils.dip2px(getContext(), 2));
this.progress = this.mycontent.findViewById(android.R.id.background);
this.textView.setText(text);
if(this.bgColor != -1){
this.mycontent.setBackgroundColor(getResources().getColor(bgColor));
}
if(this.viewColor != -1){
this.progress.setBackgroundColor(getResources().getColor(viewColor));
}
if(this.textColor != -1){
this.textView.setTextColor(getResources().getColor(textColor));
}
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
this.addView(this.mycontent, layout);
}
public void setActiveStyle(boolean isActive){
if(this.bgActColor != -1){
int color = isActive?this.bgActColor:this.bgColor;
this.mycontent.setBackgroundColor(getResources().getColor(color));
}
if(this.viewActColor != -1){
int color = isActive?this.viewActColor:this.viewColor;
this.progress.setBackgroundColor(getResources().getColor(color));
}
if(this.textActColor != -1){
int color = isActive?this.textActColor:this.textColor;
this.textView.setTextColor(getResources().getColor(color));
}
}
}
就这样 只要在使用的时候定义好点击时候字体的颜色等等就能够适用在多种场景。当然,style的属性也是要加上的
<declare-styleable name="station_top_nav">
<attr name="button_text" format="string"/>
<attr name="button_text_color" format="reference"/>
<attr name="button_text_act_color" format="reference"/>
<attr name="button_bg_color" format="reference"/>
<attr name="button_bg_act_color" format="reference"/>
<attr name="button_view_color" format="reference"/>
<attr name="button_view_act_color" format="reference"/>
</declare-styleable>