Standard Android Button with a different color

后端 未结 20 3251
误落风尘
误落风尘 2020-11-21 23:43

I\'d like to change the color of a standard Android button slightly in order to better match a client\'s branding.

The best way I\'ve found to do this so far is to c

相关标签:
20条回答
  • 2020-11-22 00:14

    Mike, you might be interested in color filters.

    An example:

    button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
    

    try this to achieve the color you want.

    0 讨论(0)
  • 2020-11-22 00:14

    The shortest solution which is working with any Android version:

    <Button
         app:backgroundTint="@color/my_color"
    

    Notes/Requirements:

    • Use the app: namespace and not the android: namespace!
    • appcompat version > 24.2.0

      dependencies { compile 'com.android.support:appcompat-v7:25.3.1' }

    Explanation:

    0 讨论(0)
  • 2020-11-22 00:15

    You can Also use this online tool to customize your button http://angrytools.com/android/button/ and use android:background="@drawable/custom_btn" to define the customized button in your layout.

    0 讨论(0)
  • 2020-11-22 00:18

    You can set theme of your button to this

    <style name="AppTheme.ButtonBlue" parent="Widget.AppCompat.Button.Colored">
     <item name="colorButtonNormal">@color/HEXColor</item>
     <item name="android:textColor">@color/HEXColor</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 00:19

    This is my solution which perfectly works starting from API 15. This solution keeps all default button click effects, like material RippleEffect. I have not tested it on lower APIs, but it should work.

    All you need to do, is:

    1) Create a style which changes only colorAccent:

    <style name="Facebook.Button" parent="ThemeOverlay.AppCompat">
        <item name="colorAccent">@color/com_facebook_blue</item>
    </style>
    

    I recommend using ThemeOverlay.AppCompat or your main AppTheme as parent, to keep the rest of your styles.

    2) Add these two lines to your button widget:

    style="@style/Widget.AppCompat.Button.Colored"
    android:theme="@style/Facebook.Button"
    

    Sometimes your new colorAccent isn't showing in Android Studio Preview, but when you launch your app on the phone, the color will be changed.


    Sample Button widget

    <Button
        android:id="@+id/sign_in_with_facebook"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/sign_in_facebook"
        android:textColor="@android:color/white"
        android:theme="@style/Facebook.Button" />
    

    0 讨论(0)
  • 2020-11-22 00:20

    Use it in this way:

    buttonOBJ.getBackground().setColorFilter(Color.parseColor("#YOUR_HEX_COLOR_CODE"), PorterDuff.Mode.MULTIPLY);
    
    0 讨论(0)
提交回复
热议问题