ToogleButton右对齐文本标签文本、标签、右对齐、ToogleButton

2023-09-08 08:50:41 作者:樱桃裙

我要对齐在 ToogleButton 文本上吧,成为绿/白时pressing的权利。

I want to align the text in a ToogleButton on the right of that bar that became green/white when pressing it.

要更加明确,我将张贴code,也是导致我现在有。

To be more explicit I will post the code and also the result I have now.

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

和的结果:

and the result:

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

I want the bar and the text to not be overlapped.

推荐答案

你试过了attibute重力?

have you tried the attibute gravity?

android:gravity="center"

之间的性差异的android:layout_gravity =中心安卓重力=中心的是,第一个将围绕你的查看你的布局中的第二将集中的,内容查看

the diference between android:layout_gravity="center" and android:gravity="center" is that the first one will center your View within your layout and the second will center the content of that View.

这是我对你的按钮布局:

This is my layout of your button:

<?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>