从切换按钮,在Android的填充去除 [英] Removing padding from toggle button in Android

查看:121
本文介绍了从切换按钮,在Android的填充去除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的Andr​​oid和环境背景为绘制一个简单的切换按钮。

I am creating a simple toggle button in android and setting background as a drawable.

<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看起来是这样的:

toggle_selector.xml looks like this:

<?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具有简单的形状绘制了一些颜色。

toggle_button_off and toggle_button_on have simple shape drawable with some color.

这是我怎么夸大这种切换按钮进入我的看法:

And this is how I am inflating this toggle button into my view:

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

问题是周围有在切换按钮上的文字实在太多填充和我不能够通过设置填充=0dp摆脱它。这些按钮的文本是动态添加,因此设定一个恒定的身高体重是没有帮助了。

The problem is there is just too much padding around the text in toggle button and I am not able to get rid of it by setting padding = "0dp". Text on these buttons are dynamically added so setting a constant height weight is not helping too.

推荐答案

我设置了​​minWidth,到了minHeight了0dp的解决方案。包裹在宽度和高度的内容。然后添加自定义填充,以切换按钮,我想要的。

I got the solution by setting minWidth, minHeight to 0dp. Wrapping content in the width and height. And then adding the custom padding to togglebutton that I want.

<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"/>

这篇关于从切换按钮,在Android的填充去除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆