能否创建一个没有文本的ToggleButton?

20
我想创建一个类似于这样的Android默认ToggleButton:enter image description here 但我想不带文字创建它。 我尝试使用以下代码:
ToggleButton tb = new ToggleButton(map);
tb.setText(null);
tb.setTextOn(null);
tb.setTextOff(null);

但是它在水平绿色条的顶部留下了一个空白空间。

我不想要那个空白空间,我只想要水平绿色条。

如何实现?

谢谢

10个回答

27

默认的ToggleButton样式中使用了两个Ninepatch (btn_toggle_off.9.pngbtn_toggle_on.9.png),导致顶部出现空白。

要想水平居中条形图标,您需要为ToggleButton创建一个新的样式,该样式将使用两个新的基于原始文件派生的Ninepatch图像。

编辑: 需要最少XML代码的方法:

  • 为您的ToggleButton创建两个Ninepatch图像,名称为:my_btn_toggle_on 和 my_btn_toggle_off

  • 在drawable文件夹中,创建my_btn_toggle.xml文件:

  • <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@drawable/my_btn_toggle_off" />
        <item android:state_checked="true" android:drawable="@drawable/my_btn_toggle_on" />
    </selector>
    
    在你的代码中,添加 tb.setBackgroundResource(R.drawable.my_btn_toggle)
    ToggleButton tb = new ToggleButton(map);
    tb.setText(null);
    tb.setTextOn(null);
    tb.setTextOff(null);
    tb.setBackgroundResource(R.drawable.my_btn_toggle)
    

你能解释一下如何使用Java代码创建那种样式并添加它吗?Pableras没有使用XML来设计Toggle按钮。 - NullPointerException
我认为如果没有使用XML是不可能实现的。(Pableras并没有明确表示他只想要Java代码)。我编辑了我的答案,提供了一种需要最少XML的方法(或许有用)。 - SteveR

20

只需要设置以下内容:

<ToggleButton android:id="@+id/tbtn1" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/bg_check"
 android:textOn="" 
 android:textOff="" 
 android:text="" />

还有,如果您需要的话,样式XML文件...

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_checked="true" 
android:drawable="@drawable/bg_check_on" /> <!-- pressed -->

  <item android:drawable="@drawable/bg_check_off" /> <!-- default/unchecked -->
</selector>

希望它有帮助~


13

使用

<ToggleButton
    ...
    android:textOff="@null"
    android:textOn="@null"
    android:textSize="0dp" />

不仅会使文字消失,还会清除其原本所在的空白区域。


1
android:textSize="0dp" 不是正确的答案。 - Gilian
完美答案。谢谢! - Sam Chen

1

像这样填写样式XML...神奇的地方在于将文本颜色设置为透明...

android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textOff="blabla"
android:textOn="blablabla"
android:textColor="@android:color/transparent"
android:background="@drawable/boutmediumrouge"

/>

在你的答案中添加一些描述,说明这个是如何工作的。 - Mathews Sunny

1

我也这么想。

android:textColor="@android:color/transparent"

0
 <ToggleButton
                android:id="@+id/setting_tb"
                style="@style/style_setting_tb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:checked="true"
                android:gravity="center"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:textColor="@color/White"
                android:textOff=""
                android:textOn=""
                android:textSize="14sp" />

0

这不是最优雅的解决方案,但您可以尝试tb.setTextSize(0);,如果您只是想隐藏顶部的额外空间。可能需要将textOn/textOff设置为空字符串:tb.setTextOn("");

编辑:更好的选择是通过extend扩展Button来创建自己的自定义按钮,保持状态(请参见此SO帖子以获取按下/未按下状态:Pressed android button state),并找到适当状态的资源进行设置...


0

使用CheckBox比使用ToggleButton更加方便。它们具有相同的状态,因此实现了相同的功能,但是您可以通过定义其<android:button="@drawable/...">属性轻松更改CheckBox的外观。


0
<ToggleButton
        android:id="@+id/tooglebutton"
        android:layout_width="60dp"
        android:layout_height="25dp"
        android:textOff="off"
        android:textOn="on"
        android:textSize="0dp"
       />

4
"请包含一些解释。" - Gnqz
1
虽然这段代码片段可能解决了问题,但包括解释真的有助于提高您的帖子质量。请记住,您正在为未来的读者回答问题,而这些人可能不知道您的代码建议原因。 - Nahuel Ianni

0

布局文件.xml

       <ToggleButton
    android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:background="@drawable/check"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

在主活动activity.java中

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ToggleButton mic = (ToggleButton)findViewById(R.id.toggle);
    mic.setTextOff(null);
    mic.setTextOn(null);
    mic.setText(null);

check.xml 被 layout.xml 使用

<?xml version="1.0" encoding="utf-8"?>

    <item android:drawable="@drawable/mic_on"
        android:state_checked="true">

    </item>
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/mic_off"
        android:state_checked="false">

    </item>

这些行就是你要找的

    mic.setTextOff(null);
    mic.setTextOn(null);
    mic.setText(null);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接