<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.button.MaterialButton
android:id="@+id/handymanServicesButton"
style="@style/View.RoundedMaterialButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:layout_weight="1"
android:elevation="16dp"
android:onClick="@{view::handymanServices}"
android:text="@{viewModel.isHandymanUser() ? @string/handyman_services_button_label : @string/main_button_label}"
android:textAllCaps="false"
android:textSize="@dimen/text_size_small"
tools:text="@string/handyman_services_button_label" />
<android.support.design.button.MaterialButton
android:id="@+id/otherButton"
style="@style/RoundedMaterialButtonNotSelected"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:layout_weight="1"
android:elevation="16dp"
android:onClick="@{view::other}"
android:text="@string/other_button_label"
android:textAllCaps="false"
android:textSize="@dimen/text_size_small" />
</LinearLayout>
我应用于按钮的样式。
<style name="View.RoundedMaterialButton">
<item name="android:minHeight">@dimen/buttonHeight</item>
<item name="android:elevation">@dimen/cardElevation</item>
<item name="android:gravity">center</item>
<item name="rippleColor">@color/material_ripple_color</item>
<item name="cornerRadius">24dp</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
<item name="backgroundTint">@color/colorAccent</item>
<item name="android:textAllCaps">true</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Button</item>
</style>
<style name="RoundedMaterialButtonNotSelected" parent="View.RoundedMaterialButton">
<item name="backgroundTint">@android:color/white</item>
<item name="android:textColor">@color/colorAccent</item>
</style>
更改颜色的代码片段。
private fun handymanServicesUi() {
binding.handymanServicesButton.backgroundTintList =
ContextCompat.getColorStateList(requireContext(), R.color.colorAccent)
binding.otherButton.backgroundTintList =
ContextCompat.getColorStateList(requireContext(), android.R.color.white)
binding.handymanServicesButton.setTextColor(Color.WHITE)
binding.otherButton.setTextColor(
ContextCompat.getColor(requireContext(), R.color.colorAccent))
}
fun handymanServices(@Suppress("UNUSED_PARAMETER") view: View) {
handymanServicesUi()
viewModel.switchToHandymanServices()
}
private fun otherUi() {
binding.handymanServicesButton.backgroundTintList =
ContextCompat.getColorStateList(requireContext(), android.R.color.white)
binding.otherButton.backgroundTintList =
ContextCompat.getColorStateList(requireContext(), R.color.colorAccent)
binding.handymanServicesButton.setTextColor(
ContextCompat.getColor(requireContext(), R.color.colorAccent))
binding.otherButton.setTextColor(Color.WHITE)
}
fun other(@Suppress("UNUSED_PARAMETER") view: View) {
otherUi()
viewModel.switchToOthers()
}



com.google.android.material.button.MaterialButton代替android.support.design.button.MaterialButton。 - underoidbackGroundTintList设置为selector可能会解决问题。 - underoid