是否可以在Button中修改textview和drawable的z顺序? [英] Is it possible to modify z-order for textview and drawable in Button?

查看:85
本文介绍了是否可以在Button中修改textview和drawable的z顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自定义按钮,该按钮扩展了复合按钮,但textview位于该按钮的可绘制对象的后面.我希望Button文本出现,而drawable应该在后面,而不在布局中创建其他视图.

I created Custom Button which extends Compound Button but the textview is behind the drawable of the button. I want text of Button should come up and drawable should go behind without creating other view in the layout.

有可能实现这一目标吗?

Is it possible to achieve this?

任何链接或示例代码都有很大帮助.

Any Link or sample code helps a lot.

推荐答案

CompoundButton是一个抽象类,主要用于从SDK创建其他视图,例如CheckboxSwitch,它具有自身的局限性和特定性行为.

CompoundButton is an abstract class which is mainly used to create other views from SDK like Checkbox or Switch and it has it's own limitations and specific behaviour.

有多种方法可以更轻松地实现它,并且没有CompoundButton依赖项.

There are different ways to implement it much easier and without CompoundButton dependency.

1.创建您自己的ViewGroup并将子级用作图层.有更改z-order的方法:view.bringToFrontgroup.bringChildToFront.此类每秒旋转三层.

1. Create your own ViewGroup and use children as layers. There're methods to change z-order: view.bringToFront and group.bringChildToFront. This class rotates three layers every second.

static class LayeredButton extends RelativeLayout {

    public LayeredButton(Context context) {
        super(context);
        Button buttonA = new Button(context);
        Button buttonB = new Button(context);
        Button buttonC = new Button(context);

        buttonA.setText("a");
        buttonB.setText("b");
        buttonC.setText("c");

        addView(buttonC);
        addView(buttonB);
        addView(buttonA);

        rotateChildren();
    }

    void rotateChildren(){
        postDelayed(new Runnable() {
            @Override
            public void run() {
                getChildAt(0).bringToFront();
                rotateChildren();
            }
        }, 1000);
    }

}

2.另一个选择就是切换视图的可见性:view.setVisibility(View.VISIBLE)也在ViewGroup

2. Another option is just to switch visibility of views: view.setVisibility(View.VISIBLE) also inside ViewGroup

3.最后一个是使用Canvas手动"绘制它.这可能是最灵活的方法.还具有更好的性能,并为您提供了添加自定义动画的机会.

3. And last one is draw it "manually" using Canvas. This is probably the most flexible way to do it. Also has better performance and gives you opportunity to add custom animation.

public void onDraw(Canvas canvas){
    canvas.drawRect(rect);//bottom layer
    canvas.drawText(text);//mid layer
    canvas.drawLine(line);//top layer
}

这篇关于是否可以在Button中修改textview和drawable的z顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆