【问题标题】:Set different color to buttons为按钮设置不同的颜色
【发布时间】:2015-12-03 12:40:47
【问题描述】:

我有 6 个按钮,想将 5 个按钮的颜色设置为橙色(#F08C35),最后一个设置为粉色(#D81B60)。我为每个样式创建了样式(styles.xml);

    <style name="MagentaButtonStyle" parent="Widget.AppCompat.Button.Colored">
        <item name="colorButtonNormal">#D81B60</item>
    </style>

    <style name="OrangeButtonStyle" parent="Widget.AppCompat.Button.Colored">
        <item name="colorButtonNormal">#F08C35</item>
    </style>

但是所有按钮都设置为colorAccent (#FF4081),因为它从parent="Widget.AppCompat.Button.Colored" 中选择强调色。

我尝试设置android:background attr,但是按钮的整个空间变为设置的背景,我失去了凸起的按钮效果。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarStyle="outsideOverlay"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.sameer.android.myappportfolio.MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:text="@string/app0"
            android:textAlignment="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="24sp" />

        <Button
            android:id="@+id/button1"
            style="@style/OrangeButtonStyle"
            android:text="@string/app1" />

        <Button
            android:id="@+id/button2"
            style="@style/OrangeButtonStyle"
            android:text="@string/app2" />

        <Button
            android:id="@+id/button3"
            style="@style/OrangeButtonStyle"
            android:text="@string/app3" />

        <Button
            android:id="@+id/button4"
            style="@style/OrangeButtonStyle"
            android:text="@string/app4" />

        <Button
            android:id="@+id/button5"
            style="@style/OrangeButtonStyle"
            android:text="@string/app5" />

        <Button
            android:id="@+id/button6"
            style="@style/MagentaButtonStyle"
            android:text="@string/app6" />

    </LinearLayout>
</ScrollView>

styles.xml 粘贴在下面:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="MagentaButtonStyle" parent="Widget.AppCompat.Button">
        <item name="colorButtonNormal">#D81B60</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:onClick">onClick</item>
    </style>
    <style name="OrangeButtonStyle" parent="Widget.AppCompat.Button">
        <item name="colorButtonNormal">#F08C35</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:onClick">onClick</item>
    </style>
</resources>

【问题讨论】:

  • 为每个按钮设置颜色可能更容易?当您希望在所有应用中应用样式时,通常会应用样式。
  • 设置你的颜色为背景
  • 正如我上面提到的,设置背景使按钮具有无边框或扁平的外观和感觉。我需要那个凸起的。
  • stackoverflow.com/questions/32542303/… 如果对你有帮助,试试这个
  • @ParthAnjaria 我曾提到过它,但它并没有达到我的目的。

标签: android android-layout android-styles


【解决方案1】:

只需使用backgroundTint,仅此而已。 此外,如果您在 pre-L 上需要它,请使用 android.support.v7.widget.AppCompatButtonapp:backgroundTint,而不是android:backgroundTint

【讨论】:

  • 谢谢@dtk12!在发布我的查询之前,我也返回了这个。但是没有成功:-)。现在也用 AppCompatButton 再次尝试
  • 您确定您使用app:backgroundTint 而不是android:backgroundTintAppCompatButton?因为对我来说效果很好。
  • 是的。我也加入了 app:XXXX。
【解决方案2】:

这对我有用:

<style name="MagentaButtonStyle" parent="Widget.AppCompat.Button">
    <item name="colorButtonNormal">#D81B60</item>
</style>

<style name="OrangeButtonStyle" parent="Widget.AppCompat.Button">
    <item name="colorButtonNormal">#F08C35</item>
</style>

您应该将父级设置为"Widget.AppCompat.Button" 而不是"Widget.AppCompat.Button.Colored"。根据您的需要,此解决方案会保留“凸起的按钮效果”。

编辑:

您应该使用这样的样式将其应用于按钮:

<Button
    android:id="@+id/button1"
    android:theme="@style/OrangeButtonStyle"
    android:text="@string/app1" />

使用属性android:theme= 而不是style=。它会起作用的。

【讨论】:

  • 谢谢我已经试过了。但是它对我不起作用。我正在模拟器上尝试。
  • @Sameer 这很奇怪,因为它应该可以工作。您可以编辑您的问题并在 xml 布局文件中显示您的按钮吗?
  • 谢谢@6franek。还没有成功!
  • 在这种情况下,与主题相比,这里的风格更有意义。
【解决方案3】:
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android"> 
   <gradient
     android:startColor="@color/drk_button"
     android:endColor="@color/lgt_button"
     android:angle="90">
   </gradient>

   <corners android:radius="20dip" />

   <stroke
     android:width="1px"
     android:color="@color/drk_button" />
</shape>

如果有帮助,试试这个。你需要设置颜色。 希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2011-12-30
    • 2013-08-30
    • 1970-01-01
    • 2018-04-20
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多