Android切换按钮-背景色

Android切换按钮-背景色,android,android-togglebutton,Android,Android Togglebutton,我尝试使用一个切换按钮,支持回到API 11。问题是这个标准的切换按钮似乎有一个灰色的背景色,而我只希望它是白色的(像背景) 我一直在努力: android:background="@color/white" android:background="@null" 但是我也失去了我想要保持的指示灯。 我已经看到了一些关于更改切换按钮的背景色(创建自己的选择器)的答案,但我似乎也失去了这些按钮的指示灯 这是布局的相关部分: <LinearLayout android

我尝试使用一个切换按钮,支持回到API 11。问题是这个标准的切换按钮似乎有一个灰色的背景色,而我只希望它是白色的(像背景)

我一直在努力:

android:background="@color/white"
android:background="@null"
但是我也失去了我想要保持的指示灯。 我已经看到了一些关于更改切换按钮的背景色(创建自己的选择器)的答案,但我似乎也失去了这些按钮的指示灯

这是布局的相关部分:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/event_bottom_sheet_attenders"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/white"
                android:drawableLeft="@drawable/state_list_people_person_bottom_sheet"
                android:drawableStart="@drawable/state_list_people_person_bottom_sheet"
                android:gravity="center"
                android:padding="10dp"
                android:text="Attenders"
                android:textColor="@color/material_teal_500" />

            <ToggleButton
                android:id="@+id/event_bottom_sheet_toggle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:gravity="center"
                android:padding="10dp"
                android:textColor="@color/material_teal_500"
                android:textOff="Not going"
                android:textOn="Going" />

</LinearLayout>

我该如何着手解决这个问题


谢谢。

你可以考虑一开始就看不同。

选择一个适合你喜欢的主题,或者如果你想要一些特别的东西,你可以定制自己的主题

根据您的描述,您可以尝试Android Light主题。更改您的活动信息,例如:

  <activity android:theme="@android:style/Theme.Light">

附言:应用主题也能让你的应用看起来更一致

看看这个:-

ToggleButton Btn=new ToggleButton(this);// or get it from the layout by ToggleButton Btn=(ToggleButton) findViewById(R.id.IDofButton);
    Btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked)
                buttonView.setBackgroundColor(Color.GREY);
            else buttonView.setBackgroundColor(Color.WHITE);
        }
    });
根据需要做相应的事情


希望这有帮助。:)

如果我这样做,我也会失去指示灯(底部的青色条)。我的活动使用了一个黑色主题,我想保留它。我显示这一点的地方是在底部的工作表对话框中。我有没有办法把一个主题应用到这上面?在这种情况下,你必须创建你的自定义绘图来设置背景。嗯,是的,我也这么认为。你知道我怎样才能找到灯光主题通常使用的可绘制图像,这样我就可以从那里复制并根据需要进行调整了吗?我不太确定,所以很抱歉it@sihao我知道我迟到了,但将其设置为background
?android:attr/selectableItemBackground
应该可以,你有没有想过解决这个问题?我尝试了
android:background=“?android:attr/selectableItemBackground”
选项,但它删除了下面的指示行。