此自定义view给出了接口mListener,实现点击事件,同时可设置左右buton的显示隐藏,调用setButtonVisable,使用枚举区分左右
1 在values目录下创建attrs.xml属性文件,定义自定义的控件属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TopBar"> <attr name="mtitle" format="string"></attr> <attr name="titleTextSize" format="dimension"></attr> <attr name="titleTextColor" format="color"></attr> <attr name="leftTextColor" format="color"></attr> <attr name="leftBackground" format="reference|color"></attr> <attr name="leftText" format="string"></attr> <attr name="rightTextColor" format="color"></attr> <attr name="rightBackground" format="reference|color"></attr> <attr name="rightText" format="string"></attr> </declare-styleable> </resources>
2 java代码部分
import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Build; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import com.speek.lxl.myview.R; /** * 作者:水东流 * 时间:2016/4/20 * 此自定义view给出了接口mListener,实现点击事件,同时可设置左右buton的显示隐藏,调用setButtonVisable,使用枚举区分左右 */ public class TopBar extends RelativeLayout { private Drawable mleftBackgroud; private int mleftTextColor; private String mleftText; private String mrightText; private Drawable mrightBackgroud; private int mrightTextColor; private float mTitleSize; private int mTitleTextColor; private String mTitle; private Button mleftButton; private Button mRightButton; private TextView mTitleView; private LayoutParams mleftParams; private LayoutParams mrightParams; private LayoutParams mTitleParams; private topbarClickListener mListener; /*此枚举用于那啥,隐藏button时区分左右*/ public enum ButonId{ left,right; } public TopBar(Context context) { this(context, null); } /*在调用处实现*/ public void setmListener(topbarClickListener mListener) { this.mListener = mListener; } public TopBar(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public TopBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } /** * 在此初始化使用的数据 * @param context * @param attrs */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void init(Context context, AttributeSet attrs) { //此方法可以将定义于attrs中的属性获取保存到typedArry TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.TopBar); //从typedArray中取出对应的值来为要设置的属性赋值 mleftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,0); mrightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor, 0); mleftBackgroud=ta.getDrawable(R.styleable.TopBar_leftBackground); mrightBackgroud=ta.getDrawable(R.styleable.TopBar_rightBackground); mleftText=ta.getString(R.styleable.TopBar_leftText); mrightText=ta.getString(R.styleable.TopBar_rightText); mTitleSize=ta.getDimension(R.styleable.TopBar_titleTextSize, 10); mTitleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor, 0); mTitle=ta.getString(R.styleable.TopBar_mtitle); //获取完typedarray后,一般要调用recyle方法来回收资源,避免重新创建的时候的错误 ta.recycle(); mleftButton=new Button(context); mRightButton=new Button(context); mTitleView=new TextView(context); //为创建的组件元素赋值 mleftButton.setText(mleftText); mleftButton.setTextColor(mleftTextColor); mleftButton.setBackground(mleftBackgroud); mRightButton.setText(mrightText); mRightButton.setTextColor(mrightTextColor); mRightButton.setBackground(mrightBackgroud); mTitleView.setTextColor(mTitleTextColor); mTitleView.setText(mTitle); mTitleView.setTextSize(mTitleSize); mTitleView.setGravity(Gravity.CENTER); //为组件元素设置相应的布局元素 mleftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); mleftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); addView(mleftButton, mleftParams); mrightParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); mrightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); addView(mRightButton, mrightParams); mTitleParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(mTitleView, mTitleParams); /*添加点击事件,借用接口,在外部实现*/ mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mListener.rightClick(); } }); mleftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mListener.leftClick(); } }); } /*設置按鈕是否显示*/ /** * * @param id 使用的枚举 * @param b */ public void setButtonVisable(ButonId id,boolean b){ switch (id){ case left: mleftButton.setVisibility(b==true?VISIBLE:GONE); break; case right: mRightButton.setVisibility(b==true?VISIBLE:GONE); break; } } public interface topbarClickListener{ void leftClick();//左边点击事件 void rightClick();//右边点击事件 } }3 ui界面使用代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:custom="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <com.speek.lxl.myview.MyView.TopBar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="40dp" custom:leftText="后退" custom:leftTextColor="@android:color/holo_blue_bright" custom:rightText="菜单" custom:rightTextColor="@android:color/background_dark" custom:rightBackground="@color/background_material_light" custom:mtitle="水东流" custom:titleTextColor="@android:color/background_dark" custom:titleTextSize="12dp" ></com.speek.lxl.myview.MyView.TopBar>
</LinearLayout>
4 在activity中的使用
TopBar tb= (TopBar) findViewById(R.id.topbar); tb.setmListener(new TopBar.topbarClickListener() { @Override public void leftClick() { Toast.makeText(MainActivity.this,"水东流欢迎你",Toast.LENGTH_SHORT).show(); } @Override public void rightClick() { Toast.makeText(MainActivity.this,"无聊啊,来2两女儿红",Toast.LENGTH_SHORT).show(); } }); tb.setButtonVisable(TopBar.ButonId.left, true); tb.setButtonVisable(TopBar.ButonId.right,true); }