我有一个线性布局,我在其中以编程方式添加按钮。我希望这些按钮具有按钮的默认外观和行为( float 、阴影等),但我想更改主要颜色。
这是我试过的
val newContext = ContextThemeWrapper(baseContext, R.style.PrimaryColoredButton)
val button = Button(newContext)
button.text = "test"
我的 styles.xml
包含以下代码
<style name="PrimaryColoredButton" parent="Base.Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">@color/colorAccent</item>
<item name="colorControlHighlight">@color/colorPrimary</item>
</style>
然而,这并没有改变我的按钮的颜色,而是给出了默认颜色。
最佳答案
你可以使用
android:backgroundTint="@android:color/white"
但遗憾的是,它只能在 Android API 21+ 中使用
如果您想更改 Android API 级别低于 21 的按钮。
您可以使用 AppCompatButton 使用应用程序命名空间而不是 android 的 backgroundTint。所以它会像这样 app:backgroundTint="@android:color/white"
。
例如:
<android.support.v7.widget.AppCompatButton
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button"
app:backgroundTint="@android:color/white" />
关于android - 添加具有默认外观但更改背景颜色的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57522652/