ToggleButton Text Label Placement for On and Off State

前端 未结 5 1807
暗喜
暗喜 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:22

      
    public class StackActivity extends Activity implements OnCheckedChangeListener {
    
        private ToggleButton tb;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            tb = (ToggleButton)findViewById(R.id.toggleButton1);
            tb.setOnCheckedChangeListener(this);
    
        }
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
            if(isChecked)
            {
                tb.setGravity(Gravity.LEFT);
                tb.setPadding(5,0,0,0);    // Set left padding 
            } else
            {
                tb.setGravity(Gravity.RIGHT);
                tb.setPadding(0,0,5,0);    // Set right padding
            }
        }
    }
    

提交回复
热议问题