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; /** * Created by YRC on 2017/4/20. */ public class TopBar extends RelativeLayout{ String title,leftText,rightText; int titleTextColor, leftTextColor, rightTextColor; float titleTextSize; Drawable leftBackground, rightBackground; Button leftButton,rightButton; TextView titleView; LayoutParams rightParams,leftParams,titleParams; topbarClickListener mListener; public TopBar(Context context, AttributeSet attrs) { super(context, attrs); //通过这个方法,将你在attrs.xml中定义的declare-styleable的所有属性的值存储到TypedArray中 TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.TopBar); //从TypedArray中取出对应的值赖伟要设置的属性赋值 title=ta.getString(R.styleable.TopBar_title); leftText=ta.getString(R.styleable.TopBar_leftText); rightText=ta.getString(R.styleable.TopBar_rightText); titleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor,0); leftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,0); rightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor,0); titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,10); leftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground); rightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground); //获取完TypedArray的值后,一般要调用recyle方法来避免重新创建的时候的错误。完成资源的回收 ta.recycle(); leftButton=new Button(context); rightButton=new Button(context); titleView=new TextView(context); //为创建的组件元素赋值,值就来源于我们在引用的xml文件中给对象属性的赋值。 leftButton.setText(leftText); leftButton.setTextColor(leftTextColor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { leftButton.setBackground(leftBackground); } rightButton.setText(rightText); rightButton.setTextColor(rightTextColor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { rightButton.setBackground(rightBackground); } titleView.setText(title); titleView.setTextColor(titleTextColor); titleView.setTextSize(titleTextSize); titleView.setGravity(Gravity.CENTER); //为组件元素设置相应的布局元素 leftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); addView(leftButton,leftParams); rightParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); addView(rightButton,rightParams); titleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(titleView,titleParams); rightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mListener.rightClick(); } }); leftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mListener.leftClick(); } }); } public TopBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public interface topbarClickListener{ void leftClick(); void rightClick(); } //暴露一个方法给调用者来注册接口回调 //通过接口来获得回调者对接口方法的实现 public void setOnTopBarClickListener(topbarClickListener mListener){ this.mListener=mListener; } public void setButtonVisable(int id,boolean flag){ if (flag){ if (id==0){ leftButton.setVisibility(View.VISIBLE); }else { rightButton.setVisibility(View.VISIBLE); } }else { if (id==0){ leftButton.setVisibility(View.GONE); }else { rightButton.setVisibility(View.GONE); } } } }
工具类/自定义顶栏
最新推荐文章于 2020-10-25 03:08:41 发布