【问题标题】:Adding a button with default appearance but changed background color添加具有默认外观但更改了背景颜色的按钮
【发布时间】:2019-08-16 10:01:12
【问题描述】:

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

这是我尝试过的

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 material-design android-button material-components material-components-android


    【解决方案1】:

    你可以使用 android:backgroundTint="@android:color/white" 但遗憾的是,它只能在 Android API 21+ 中使用

    如果您想更改低于 21 的 Android API 级别的按钮。 您可以使用 appCompatButton 使用 app 命名空间而不是 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" />
    

    【讨论】:

      【解决方案2】:

      试试这样的形状背景。

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

      【讨论】:

        【解决方案3】:

        使用新的Material Button,您有两种选择:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-26
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          • 2013-02-17
          • 2015-06-04
          • 1970-01-01
          相关资源
          最近更新 更多