在android中重叠togglebutton上的文本

时间:2016-09-22 09:52:37

标签: java android togglebutton

我已经以编程方式创建了一个切换按钮并使用它但是当它在设备上运行时,切换按钮的状态在切换按钮上重叠。如图像enter image description here

切换的代码如下所示

  ToggleButton tb[];
            tb=new ToggleButton[len];
            tb[i]=new ToggleButton(this);

            LinearLayout.LayoutParams tbparams = new LinearLayout.LayoutParams
                    (90, LinearLayout.LayoutParams.WRAP_CONTENT);
            tbparams.setMargins(20, 0, 0, 0);
            tb[i].setBackgroundResource(R.drawable.bgtoggle);
            tb[i].setId(i+1);
            tb[i].setTextOff(" ");
            tb[i].setTextOn(" ");
            tb[i].setTag(stringList.get(i));
            tb[i].setPadding(4, 4, 4, 4);
            tb[i].setLayoutParams(tbparams);
            layout.addView(tb[i]);

    tb[i].setOnCheckedChangeListener(handleOnClick(
            tb[i], tb[i].getId(),tb[i].getTag(),stringList));

bgtoggle.xml

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

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



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

    <item android:drawable="@drawable/off" android:state_checked="false"/>
    <item android:drawable="@drawable/off"></item>-->

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

    <item android:drawable="@drawable/offsw" android:state_checked="false"/>
    <item android:drawable="@drawable/offsw"></item>


</selector>

请帮助

2 个答案:

答案 0 :(得分:2)

谢谢大家,我通过设置textcolor tranparent

来纠正它
color value= #00ffffff

答案 1 :(得分:0)

如果在布局中添加切换按钮,它将按预期显示。但在您的情况下,您可以动态创建它,您可以添加

TB [I] .performClick();

在您将其添加到线性布局之前。