Android 可以创建一个没有文本的 ToggleButton 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11604476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
It is possible to create a ToggleButton without text?
提问by Pableras84
i want to create a android default ToggleButton
like this:
我想像这样创建一个android默认值ToggleButton
:
but i want to create it without TEXT
但我想在没有文本的情况下创建它
I tryed with this code:
我尝试使用此代码:
ToggleButton tb = new ToggleButton(map);
tb.setText(null);
tb.setTextOn(null);
tb.setTextOff(null);
But it is leaving a empty space in the top of the horizontal green bar.
但它在水平绿色条的顶部留下了一个空白空间。
I dont want that empty space, i only want the horizontal green bar.
我不想要那个空的空间,我只想要水平的绿色条。
How to achieve it?
如何实现?
thanks
谢谢
回答by SteveR
The empty space in the top is caused by the 2 Ninepatch (btn_toggle_off.9.png
and btn_toggle_on.9.png
) used in default toggleButton style.
顶部的空白是由默认切换按钮样式中使用的 2 Ninepatch (btn_toggle_off.9.png
和btn_toggle_on.9.png
)引起的。
To perfectly center the horizontal bar, you must create a new style for ToggleButton that will use two new ninepatch derived form original.
为了使水平条完美居中,您必须为 ToggleButton 创建一个新样式,该样式将使用两个新的 Ninepatch 派生形式原始。
Edit: Method requiring minimal XML:
编辑:需要最少 XML 的方法:
create your two ninepatches for your togglebutton, name it:
my_btn_toggle
_on andmy_btn_toggle_off
in drawable folder, create
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>
in your code, add
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)
为切换按钮创建两个九块,命名为:
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)
回答by RRTW
Just set following things:
只需设置以下内容:
<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="" />
And, the style xml, if you need...
而且,样式 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>
Hope it helps~
希望有帮助~
回答by Tad
Using
使用
<ToggleButton
...
android:textOff="@null"
android:textOn="@null"
android:textSize="0dp" />
will not only make the text go away, but also get rid of the empty space where it would otherwise be shown.
不仅会使文本消失,还会消除原本会显示的空白区域。
回答by Сергей Светлов
I thought so
我是这么想的
android:textColor="@android:color/transparent"
回答by PJ_Finnegan
It's much more hassle-free to use a CheckBox
instead of the ToggleButton
. They have the same states and thus achieve the same functionality, but you can easily change the CheckBox
appearence by defining its <android:button="@drawable/...">
attribute.
使用 aCheckBox
而不是ToggleButton
. 它们具有相同的状态并因此实现相同的功能,但您可以CheckBox
通过定义其<android:button="@drawable/...">
属性轻松更改外观。
回答by Sanjay Goswami
<ToggleButton
android:id="@+id/tooglebutton"
android:layout_width="60dp"
android:layout_height="25dp"
android:textOff="off"
android:textOn="on"
android:textSize="0dp"
/>
回答by Marc Sauvageau
Fill style XML like this.. the magic is text color as transparent...
像这样填充样式 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"
/>
回答by Ramandeep Singh
Layout File.xml
布局文件.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" />
In main activity.java
在主要活动.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 which is used by layout.xml
layout.xml 使用的 check.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>
These lines are those what you are looking for
这些线是您正在寻找的
mic.setTextOff(null);
mic.setTextOn(null);
mic.setText(null);
回答by Salil Pandit
Not the most elegant solution but you could try tb.setTextSize(0);
if you're just trying to hide the extra space on top. May need to set the textOn/textOff to an empty string: tb.setTextOn("");
不是最优雅的解决方案,但tb.setTextSize(0);
如果您只是想隐藏顶部的额外空间,您可以尝试。可能需要将 textOn/textOff 设置为空字符串:tb.setTextOn("");
EDIT: I better option would be to create your own custom button by extend
ing Button
, hold the state (see this SO post for pressed/unpressed states: Pressed android button state) and find the assets to set as the appropriate state...
编辑:我更好的选择是通过extend
ing创建您自己的自定义按钮Button
,保持状态(请参阅此 SO 帖子以了解按下/未按下状态:按下 android 按钮状态)并找到要设置为适当状态的资产...
回答by Faakhir
<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" />