我正在 android 中创建一个简单的切换按钮并将背景设置为可绘制对象。
<ToggleButton
android:layout_width="wrap_content"
android:drawablePadding="0dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:padding="0dp"
android:id="@+id/tag_text"
android:background="@drawable/toggle_selector"/>
toggle_selector.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_button_off" android:state_checked="false"/>
<item android:drawable="@drawable/toggle_button_on" android:state_checked="true"/>
</selector>
toggle_button_off 和toggle_button_on 具有可绘制的简单形状,并具有某种颜色。
这就是我将这个切换按钮扩展到我的视图中的方式:
View child = getLayoutInflater().inflate(R.layout.tags, null);
ToggleButton tag = ((ToggleButton)child.findViewById(R.id.tag_text));
tag.setText("Testing");
tag.setTextOff("Testing");
tag.setTextOn("Testing");
flowlayout.addView(child);
问题是切换按钮中的文本周围有太多填充,我无法通过设置来摆脱它padding = "0dp"
。这些按钮上的文本是动态添加的,因此设置恒定的高度重量也没有帮助。
我通过将 minWidth、minHeight 设置为 0dp 得到了解决方案。将内容包裹在宽度和高度中。然后将自定义填充添加到我想要的切换按钮。
<ToggleButton
android:layout_width="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:padding="2dp"
android:id="@+id/tag_text"
android:background="@drawable/toggle_selector"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)