![enter image description here](https://istack.dev59.com/NzQ2k.webp)
ToggleButton tb = new ToggleButton(map);
tb.setText(null);
tb.setTextOn(null);
tb.setTextOff(null);
但是它在水平绿色条的顶部留下了一个空白空间。
我不想要那个空白空间,我只想要水平绿色条。
如何实现?
谢谢
ToggleButton tb = new ToggleButton(map);
tb.setText(null);
tb.setTextOn(null);
tb.setTextOff(null);
但是它在水平绿色条的顶部留下了一个空白空间。
我不想要那个空白空间,我只想要水平绿色条。
如何实现?
谢谢
默认的ToggleButton样式中使用了两个Ninepatch (btn_toggle_off.9.png
和 btn_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)
只需要设置以下内容:
<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>
希望它有帮助~
使用
<ToggleButton
...
android:textOff="@null"
android:textOn="@null"
android:textSize="0dp" />
不仅会使文字消失,还会清除其原本所在的空白区域。
像这样填写样式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"
/>
我也这么想。
android:textColor="@android:color/transparent"
<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" />
这不是最优雅的解决方案,但您可以尝试tb.setTextSize(0);
,如果您只是想隐藏顶部的额外空间。可能需要将textOn/textOff设置为空字符串:tb.setTextOn("");
编辑:更好的选择是通过extend
扩展Button
来创建自己的自定义按钮,保持状态(请参见此SO帖子以获取按下/未按下状态:Pressed android button state),并找到适当状态的资源进行设置...
使用CheckBox
比使用ToggleButton
更加方便。它们具有相同的状态,因此实现了相同的功能,但是您可以通过定义其<android:button="@drawable/...">
属性轻松更改CheckBox
的外观。
<ToggleButton
android:id="@+id/tooglebutton"
android:layout_width="60dp"
android:layout_height="25dp"
android:textOff="off"
android:textOn="on"
android:textSize="0dp"
/>
布局文件.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);