更改 android 个按钮的背景颜色
change background color on android buttons
我想让我的按钮具有我指定的颜色,但在外观和行为上仍然像标准按钮一样。所以我搜索并找到了this。我听从了那里给出的建议,但背景颜色保持标准的灰色。知道我哪里出错了吗?
在我的 styles.xml 我把这个:
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:colorButtonNormal">@color/colorButtonDark</item>
<item name="android:textColor">@color/colorAccent</item>
</style>
我的颜色是:
<resources>
[...]
<color name="colorAccent">#ffffff</color>
<color name="colorButtonDark">#300a03</color>
</resources>
一个示例按钮如下所示:
<Button
android:text="exampleButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button7" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="@+id/guideline19" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:theme="@style/AppTheme.Button"/>
尝试在 xml 中使用 android.support.v7.widget.AppCompatButton
而不是 Button
。
<android.support.v7.widget.AppCompatButton
android:text="exampleButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button7" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="@+id/guideline19"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:theme="@style/AppTheme.Button"/>
而不是
android:theme="@style/AppTheme.Button"
使用
android:style="@style/AppTheme.Button"
.
编辑 1:
Theme vs Style
So what exactly is the difference? Well they are both
declared in exactly the same way (which you already know), the
difference comes in how they’re used.
Themes are meant to be the global source of styling for your app. The
new functionality doesn’t change that, it just allows you to tweak it
per view.
Styles are meant to be applied at a view level. Internally, when you
set style on a View, the LayoutInflater will read the style and apply
it to the AttributeSet before any explicit attributes (this allows you
to override style values on a view).
Values in an attribute set can reference values from the View’s theme.
TL;DR: Themes are global, styles are local.
我想让我的按钮具有我指定的颜色,但在外观和行为上仍然像标准按钮一样。所以我搜索并找到了this。我听从了那里给出的建议,但背景颜色保持标准的灰色。知道我哪里出错了吗?
在我的 styles.xml 我把这个:
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:colorButtonNormal">@color/colorButtonDark</item>
<item name="android:textColor">@color/colorAccent</item>
</style>
我的颜色是:
<resources>
[...]
<color name="colorAccent">#ffffff</color>
<color name="colorButtonDark">#300a03</color>
</resources>
一个示例按钮如下所示:
<Button
android:text="exampleButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button7" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="@+id/guideline19" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:theme="@style/AppTheme.Button"/>
尝试在 xml 中使用 android.support.v7.widget.AppCompatButton
而不是 Button
。
<android.support.v7.widget.AppCompatButton
android:text="exampleButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button7" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="@+id/guideline19"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:theme="@style/AppTheme.Button"/>
而不是
android:theme="@style/AppTheme.Button"
使用
android:style="@style/AppTheme.Button"
.
编辑 1:
Theme vs Style
So what exactly is the difference? Well they are both declared in exactly the same way (which you already know), the difference comes in how they’re used.
Themes are meant to be the global source of styling for your app. The new functionality doesn’t change that, it just allows you to tweak it per view.
Styles are meant to be applied at a view level. Internally, when you set style on a View, the LayoutInflater will read the style and apply it to the AttributeSet before any explicit attributes (this allows you to override style values on a view).
Values in an attribute set can reference values from the View’s theme.
TL;DR: Themes are global, styles are local.