添加具有默认外观但更改背景颜色的按钮

Adding a button with default appearance but changed background color

我有一个线性布局,我在其中以编程方式添加按钮。我希望这些按钮具有按钮的默认外观和行为(浮动、阴影等),但我想更改主要颜色。

这是我试过的

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

试试这样的形状背景。

<ImageButton
      android:id="@+id/button1"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:layout_gravity="center"
      android:background="@drawable/btn_bg"
      android:stateListAnimator="@anim/btn_elevation" 
      android:layout_margin="8dp"
      android:elevation="1dp"
      android:text="My Button"
      android:layout_marginRight="5dp" />

@drawable/btn_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">

 <!-- this will change your button bg -->
   <solid
        android:color="@color/color_white" />

<!-- this will give your button a surrounding stroke -->
<stroke
        android:width="1dp"
        android:color="@color/color_accent" />

 <!-- this will give your button rounded corners -->
   <corners
        android:radius="10dp" />

</shape>

@anim/btn_elevation.xml(如果您不需要,请从主按钮中删除):

  <?xml version="1.0" encoding="utf-8"?>
  <selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:state_enabled="true"
    android:state_pressed="true">

    <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueFrom="2dp"
        android:valueTo="6dp"
        android:valueType="floatType" />
</item>

<item>
    <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueFrom="6dp"
        android:valueTo="2dp"
        android:valueType="floatType" />
 </item>

  </selector>

有了新的 Material Button,您有 2 个选择:

  1. 使用 backgroundTint 属性。

类似于:

<style name="MyButtonStyle"
 parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/button_selector</item>
    //..
</style>
  1. 这是我认为的最佳选择。如果你想覆盖默认样式的一些主题属性,那么你可以使用新的 materialThemeOverlay 属性。

类似于:

<style name="MtButtonStyle"
 parent="Widget.MaterialComponents.Button">
   <item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
</style>

<style name="GreenButtonThemeOverlay">
  <item name="colorPrimary">@color/green</item>
</style>

需要'com.google.android.material:material:1.1.0'.