a_s*_*ber 7 android android-button material-design material-components material-components-android
Android Studio 3.2.1这是我的布局:
<com.google.android.material.button.MaterialButton
android:id="@+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="@dimen/min_height"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:text="@string/json_view"
app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />
Run Code Online (Sandbox Code Playgroud)
更改MaterialButton的背景,我在styles.xml中更改colorAccent
<item name="colorAccent">@color/colorAccent</item>
Run Code Online (Sandbox Code Playgroud)
真好 是工作。
但是问题是:我不想更改colorAccent。我想为MaterialButton与colorAccent使用不同的背景颜色
属性:
android:background="#aabbcc"
Run Code Online (Sandbox Code Playgroud)
没有帮助。
Gab*_*tti 34
有了MaterialButton2 个选项:
使用Zaid MirzabackgroundTint建议的属性
如果要覆盖默认样式的某些主题属性,可以使用新materialThemeOverlay属性。在我看来,这是最好的选择。
就像是:
<style name="Widget.App.ButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">@style/GreenButtonThemeOverlay</item>
</style>
<style name="GreenButtonThemeOverlay">
<item name="colorPrimary">@color/green</item>
</style>
Run Code Online (Sandbox Code Playgroud)
进而:
<com.google.android.material.button.MaterialButton
style="Widget.App.ButtonStyle"
../>
Run Code Online (Sandbox Code Playgroud)
它至少需要库的1.1.0版本。
小智 25
如果要设置自定义可绘制对象,则需要将app:backgroundTint="@null". 仅用于更改背景颜色app:backgroundTint="@color/yourColor"
我目前正在使用 1.3.0-alpha01
<com.google.android.material.button.MaterialButton
android:id="@+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="@dimen/min_height"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
app:backgroundTint="@null"
android:background="@drawable/your_custom_drawable"
android:text="@string/json_view"
app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />
Run Code Online (Sandbox Code Playgroud)
Abd*_*een 12
您可以按照下面的代码来完成。
android:background="@color/black"
app:backgroundTint="@null"
Run Code Online (Sandbox Code Playgroud)
第一种解决方案
您可以使用app:backgroundTint更改背景色MaterialButton
<com.google.android.material.button.MaterialButton
android:id="@+id/bittrexJsonViewButton"
android:layout_width="0dp"
android:layout_height="@dimen/min_height"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
app:backgroundTint="@android:color/holo_orange_dark"
android:text="@string/json_view"
app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />
Run Code Online (Sandbox Code Playgroud)
第二解决方案
MaterialButton使用colorPrimary作为背景当按钮处于活动状态,并且colorOnSurface在禁用时。因此,您可以在主题中定义它并将其应用到材质按钮上
| 归档时间: |
|
| 查看次数: |
1763 次 |
| 最近记录: |