ToggleButton Text Label Placement for On and Off State

前端 未结 5 1941
暗喜
暗喜 2021-01-03 00:38

Is there anyway to control the text position for a ToggleButton\'s on and off state? For instance, I want the text label to be left aligned when on and right aligned when of

5条回答
  •  渐次进展
    2021-01-03 01:38

    Change the alignment by changing the gravity whenever the button is clicked by adding some code in the OnClickListener like this:

    toggleButton.setOnClickListener(new OnClickListener() 
            {
                public void onClick(View v) {
                   if (((ToggleButton)v).isChecked())
                        toggleButton.setGravity(Gravity.LEFT);
                   else
                       toggleButton.setGravity(Gravity.RIGHT);
                }
            });
    

提交回复
热议问题