android自定义组件

第一:自定义按钮
attr.xml中定义属性

    <declare-styleable name="MyRadioButton">
        <attr name="mDrawableSize" format="dimension"/>
        <attr name="mDrawableTop" format="reference"/>
        <attr name="mDrawableLeft" format="reference"/>
        <attr name="mDrawableRight" format="reference"/>
        <attr name="mDrawableBottom" format="reference"/>
    </declare-styleable>

定义BottomRadioButton继承RadioButton实现一下方法

    public BottomRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
        //获取attrs中定义的属性
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyRadioButton);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            Log.i("MyRadioButton", "attr:" + attr);
            switch (attr) {
                case R.styleable.MyRadioButton_mDrawableSize:
                //获取字体大小
                    mDrawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_mDrawableSize, 50);
                    Log.i("MyRadioButton", "mDrawableSize:" + mDrawableSize);
                    break;
                case R.styleable.MyRadioButton_mDrawableTop:
                    drawableTop = a.getDrawable(attr);
                    break;
                case R.styleable.MyRadioButton_mDrawableBottom:
                    drawableRight = a.getDrawable(attr);
                    break;
                case R.styleable.MyRadioButton_mDrawableRight:
                    drawableBottom = a.getDrawable(attr);
                    break;
                case R.styleable.MyRadioButton_mDrawableLeft:
                    drawableLeft = a.getDrawable(attr);
                    break;
                default :
                    break;
            }
        }
        //资源回收利用
        a.recycle();

        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);

    }

//设置属性
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left,
                                                        Drawable top, Drawable right, Drawable bottom) {

        if (left != null) {
            left.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (right != null) {
            right.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (top != null) {
            top.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        setCompoundDrawables(left, top, right, bottom);
    }

//布局文件中应用
注意:需要再布局文件中设置别名
xmlns:app=”http://schemas.android.com/apk/res-auto”
app

<com.example.news.plug.BottomRadioButton
        android:id="@+id/bottom_home"
        android:text="@string/home"
        style="@style/bottom_button_style"
        app:mDrawableTop="@drawable/b_new_home"
        app:mDrawableSize="28dp"
        />   

第二:自定义顶部导航
顶部导航分为左部按钮、右部按钮已经中间标题部分

attr.xml 
<declare-styleable name="TopBar">
        <attr name="top_title" format="string"/>
        <attr name="top_titleTextSize" format="dimension"/>
        <attr name="top_titleTextColor" format="color"/>

    <attr name="leftText" format="string"/>
    <attr name="leftTextColor" format="color"/>
    <attr name="leftTextBackground" format="reference|color"/>

    <attr name="rightText" format="string"/>
    <attr name="rightTextColor" format="color"/>
    <attr name="rightTextBackground" format="reference|color"/>
    </declare-styleable>
TopBar extends RelativeLayout 
   private Button leftButton,rightButton;
    private TextView tvText;

    private String leftText,rightText;
    private int leftTextColor,rightTextColor;
    private Drawable leftTextBackground,rightTextBackground;

    private String title;
    private float titleTextSize;
    private int titleTextColor;

    private LayoutParams leftParams,rightParams,titleParams;

    //事件监听
    private topBarClickListener listener;
    public interface topBarClickListener{
        public void leftOnClick();
        public void rightOnClick();
    }
    public void setTopBarListener(topBarClickListener listener){
        this.listener = listener;
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);

        //获取相关参数的值
        leftText = ta.getString(R.styleable.TopBar_leftText);
        leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
        leftTextBackground = ta.getDrawable(R.styleable.TopBar_leftTextBackground);

        rightText = ta.getString(R.styleable.TopBar_rightText);
        rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
        rightTextBackground = ta.getDrawable(R.styleable.TopBar_rightTextBackground);

        title = ta.getString(R.styleable.TopBar_top_title);
        titleTextSize = ta.getDimension(R.styleable.TopBar_top_titleTextSize,0);
        titleTextColor = ta.getColor(R.styleable.TopBar_top_titleTextColor,0);

        ta.recycle();
        //赋值
        leftButton = new Button(context);
        rightButton = new Button(context);
        tvText = new TextView(context);

        leftButton.setText(leftText);
        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftTextBackground);

        rightButton.setText(rightText);
        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightTextBackground);

        tvText.setText(title);
        tvText.setTextSize(titleTextSize);
        tvText.setTextColor(titleTextColor);
        tvText.setGravity(Gravity.CENTER);

//        setBackgroundColor(0xFFF59563);

        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(tvText,titleParams);

        //添加点击事件
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.leftOnClick();
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.rightOnClick();
            }
        });
    }

应用:

      TopBar topBar = (TopBar) getActivity().findViewById(R.id.top_bar);
        topBar.setTopBarListener(new TopBar.topBarClickListener() {
            @Override
            public void leftOnClick() {
                Toast.makeText(getContext(), "左边测试", Toast.LENGTH_LONG).show();
            }

            @Override
            public void rightOnClick() {
                Toast.makeText(getContext(), "右边测试", Toast.LENGTH_LONG).show();
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值