android自定义控件之TopBar

步骤:1.res->values目录下新建attrs.xml文件。

            2.新建一个类继承自ViewGroup

<resources>
    <declare-styleable name="TopBar">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="integer"/>
        <attr name="titleTextColor" format="color"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>
public class TopBar extends RelativeLayout {
    //包含topbar上的元素:左按钮,右按钮,标题
    private Button mLeftButton,mRightButton;
    private TextView mTitleView;
    //布局属性,用来控制组建元素在viewgroup中的位置,
    private LayoutParams mLeftParams,mTitleParams,mRightParams;
    //左按钮的属性值
    private int mLeftTextColor;
    private Drawable mleftBackground;
    private String mLeftText;
    //右按钮属性
    private int mRightTextColor;
    private Drawable mRightBackground;
    private String mRightText;
    //标题的属性值
    private float mTitleTextSize;
    private int mTitleTextColor;
    private String mTitle;

    //映射传入的借口对象
    private TopbarClickListener mListener;

    public interface TopbarClickListener{
        //左按钮点击事件
        void leftClick();
        //右按钮点击事件
        void rightClick();
    }

    public void setOnTopbarClickListener(TopbarClickListener mListener){
        this.mListener=mListener;
    }

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //设置topbar背景
        setBackgroundColor(0xFFF59563);
        //将atts.xml中定义的declare-styleable的所有属性的值存储到TypedArray中
        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        //从TypedArray中读取出对应的值来要为设置的属性赋值
        mLeftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,0);
        mleftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
        mLeftText=ta.getString(R.styleable.TopBar_leftText);
        mRightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor,0);
        mRightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
        mRightText=ta.getString(R.styleable.TopBar_rightText);
        mTitleTextSize=ta.getInteger(R.styleable.TopBar_titleTextSize,10);
        mTitleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor,0);
        mTitle=ta.getString(R.styleable.TopBar_title);
        //获取完TypedArray的值后,一般要调用recycle方法来避免重新创建的时候的错误。
        ta.recycle();
        mLeftButton=new Button(context);
        mRightButton=new Button(context);
        mTitleView=new TextView(context);
        //为创建的组件元素赋值,值来源于我们在引用的xml文件中给对应出行的赋值
        mLeftButton.setTextColor(mLeftTextColor);
        mLeftButton.setBackground(mleftBackground);
        mLeftButton.setText(mLeftText);

        mRightButton.setTextColor(mRightTextColor);
        mRightButton.setBackground(mRightBackground);
        mRightButton.setText(mRightText);

        mTitleView.setText(mTitle);
        mTitleView.setTextColor(mTitleTextColor);
        mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTitleTextSize);
        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,TRUE);
        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 view) {
                mListener.rightClick();
            }
        });

        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.leftClick();
            }
        });
    }

    //设置按钮的显示与否,通过id区分按钮,flag区分是否显示
    public void setButtonVisable(int id,boolean flag){
        if (flag){
            if (id==0){
                mLeftButton.setVisibility(VISIBLE);
            }else {
                mRightButton.setVisibility(VISIBLE);
            }
        }else {
            if (id==0){
                mLeftButton.setVisibility(GONE);
            }else {
                mRightButton.setVisibility(GONE);
            }
        }
    }
}
public class MainActivity extends AppCompatActivity {
    private TopBar mTopbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTopbar=findViewById(R.id.topBar);
        //为topBar注册监听事件
        mTopbar.setOnTopbarClickListener(new TopBar.TopbarClickListener() {
            @Override
            public void leftClick() {
                Toast.makeText(MainActivity.this,
                        "right", Toast.LENGTH_SHORT)
                        .show();
            }

            @Override
            public void rightClick() {
                Toast.makeText(MainActivity.this,
                        "left", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        // 控制topbar上组件的状态
        mTopbar.setButtonVisable(0, true);
        mTopbar.setButtonVisable(1, false);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的纺织品企业财务管理系统,源码+数据库+毕业论文+视频演示 在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行更好的维护,纺织品企业财务管理系统的出现就变得水到渠成不可缺少。通过对纺织品企业财务管理系统的开发,不仅仅可以学以致用,让学到的知识变成成果出现,也强化了知识记忆,扩大了知识储备,是提升自我的一种很好的方法。通过具体的开发,对整个软件开发的过程熟练掌握,不论是前期的设计,还是后续的编码测试,都有了很深刻的认知。 纺织品企业财务管理系统通过MySQL数据库与Spring Boot框架进行开发,纺织品企业财务管理系统能够实现对财务人员,员工,收费信息,支出信息,薪资信息,留言信息,报销信息等信息的管理。 通过纺织品企业财务管理系统对相关信息的处理,让信息处理变的更加的系统,更加的规范,这是一个必然的结果。已经处理好的信息,不管是用来查找,还是分析,在效率上都会成倍的提高,让计算机变得更加符合生产需要,变成人们不可缺少的一种信息处理工具,实现了绿色办公,节省社会资源,为环境保护也做了力所能及的贡献。 关键字:纺织品企业财务管理系统,薪资信息,报销信息;SpringBoot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值