切换按钮文本标签放置的开启和关闭状态 [英] ToggleButton Text Label Placement for On and Off State
本文介绍了切换按钮文本标签放置的开启和关闭状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
反正是有控制的切换按钮的开启和关闭状态的文本位置?举例来说,我想留在文本标签对齐时,上,右对齐时关闭。
编辑: 另外,我最好包括一个小填充左侧和右侧的文本。关于5DP。并更好地控制标签位置,如果可能的。
解答: 这就是我所需要的!
button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0,0,5,0);
解决方案
公共类StackActivity扩展活动实现OnCheckedChangeListener {
私人切换按钮TB;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TB =(切换按钮)findViewById(R.id.toggleButton1);
tb.setOnCheckedChangeListener(本);
}
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked)
{
如果(器isChecked)
{
tb.setGravity(Gravity.LEFT);
tb.setPadding(5,0,0,0); //设置左填充
} 其他
{
tb.setGravity(Gravity.RIGHT);
tb.setPadding(0,0,5,0); //设置右填充
}
}
}
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 off.
EDIT: Also, I'd to include a little padding for the text on the left and right. About 5dp. and have finer control over the label placement if possible.
ANSWER: This is what I needed!
button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);
解决方案
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
}
}
}
这篇关于切换按钮文本标签放置的开启和关闭状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文