0

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);

そして結果: ここに画像の説明を入力

バーとテキストが重ならないようにしたい。

4

2 に答える 2

2

あなたは属性の重力を試しましたか?

android:gravity="center"

android:layout_gravity="center"との違いandroid:gravity="center"は、最初のものはあなたのView中であなたを中心に置きlayout、2 番目のものはその内容を中心に置くこと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>
于 2012-06-18T16:33:41.830 に答える