切换按钮对齐右侧的文本标签

时间:2012-06-18 14:27:13

标签: android

我希望在按下它时变为绿色/白色的条形图右侧的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 个答案:

答案 0 :(得分:2)

你尝试过重力吗?

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> }。

这是我的按钮布局:

{{1}}

答案 1 :(得分:1)

您正在查看android:drawableLeft="@drawable/the_button_resource"

还有其他选项,例如drawableRight / drawableTop / drawableBottom,它将可绘制资源放在参数上

相关问题