2012-06-18 58 views
0

我想对齐该栏右侧的ToogleButton中的文本,该文本在按下时变为绿色/白色。ToogleButton对齐右侧的文本标签

为了更明确我会发布代码和我现在的结果。

<ToggleButton android:id="@+id/ToggleButton01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        layout_gravity="center_vertical" 
        android:textOff="Off" 
       android:textOn="On"/> 

ToggleButton onOffButton = (ToggleButton) findViewById(R.id.ToggleButton01); 
onOffButton.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
onOffButton.setPadding(0, 0, 5, 0); 

和结果: enter image description here

我想酒吧和文字不能重复。

回答

2

你试过了attibute gravity吗?

android:gravity="center" 

android:layout_gravity="center"android:gravity="center"之间的性差异是第一个将集中你的View内的layout和第二中心将是View的内容。

这是我对你的按钮的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ToggleButton android:id="@+id/ToggleButton01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:textOff="Off" 
        android:gravity="center" 
       android:textOn="On"/> 

</LinearLayout> 
+0

它不起作用..没有效果。 – Paul

+0

好吧,我测试了它,所以你可能会重写代码中某处的值。 – Raykud

+0

不工作......基本上我想要的是(因为也许我没有表达得足够精确)是与绿线水平对齐的文本(文本 - On/Off在绿线右侧)。 – Paul

1

你正在寻找android:drawableLeft="@drawable/the_button_resource"

有喜欢drawableRight/drawableTop/drawableBottom,在帕拉姆地方绘制资源等选项建议

+0

请注意切换颜色状态的变化必须在状态xml中处理 - 即(the_button_resource)包含在“pressed的状态”,“on_state”等时使用的资源。 – takrishna