Is It Possible To Modify Z-order For Textview And Drawable In Button?
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 with
Solution 1:
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.
There are different ways to implement it much easier and without CompoundButton
dependency.
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.
staticclassLayeredButtonextendsRelativeLayout {
publicLayeredButton(Context context) {
super(context);
ButtonbuttonA=newButton(context);
ButtonbuttonB=newButton(context);
ButtonbuttonC=newButton(context);
buttonA.setText("a");
buttonB.setText("b");
buttonC.setText("c");
addView(buttonC);
addView(buttonB);
addView(buttonA);
rotateChildren();
}
voidrotateChildren(){
postDelayed(newRunnable() {
@Overridepublicvoidrun() {
getChildAt(0).bringToFront();
rotateChildren();
}
}, 1000);
}
}
2. Another option is just to switch visibility of views: view.setVisibility(View.VISIBLE)
also inside ViewGroup
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 layercanvas.drawText(text);//mid layercanvas.drawLine(line);//top layer
}
Post a Comment for "Is It Possible To Modify Z-order For Textview And Drawable In Button?"