简体   繁体   English

在android中的togglebutton上重叠文本

[英]Overlapping text over togglebutton in android

I have created a toggle button programmatically and using it but when it is running on device ,the status of toggle button is overlapping over the toggle button.like in the image 我已经以编程方式创建并使用了切换按钮,但是当它在设备上运行时,切换按钮的状态在切换按钮上重叠。 在此处输入图片说明

the code for toggle is given below 切换代码如下

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

please help 请帮忙

谢谢大家,我通过设置textcolor tranparent纠正了它

color value= #00ffffff

If you add the toggle button in the layout then it will display as you expect it to be. 如果在布局中添加切换按钮,则它将按预期显示。 But in your case, where you are creating it dynamically, you can add 但是,在您要动态创建的情况下,您可以添加

tb[i].performClick(); TB [I] .performClick();

just before you add it in your linear layout. 在将其添加到线性布局中之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM