Gia*_*nMS 3 android gradient material-components-android
我一直在尝试从Android的Material Components中的Material Button中设置渐变背景,但是它不起作用。那么,如何在“材质”按钮中设置渐变背景?
Gab*_*tti 24
从版本开始,1.2.0-alpha06您可以android:background在MaterialButton.
<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/button_gradient"
... />
Run Code Online (Sandbox Code Playgroud)
否则,如果您不能使用 1.2.0-alpha06 或更高版本,则必须使用该AppCompatButton组件。
只是一些关于MaterialButton.
backgroundTint仍然是默认的 MaterialButton 样式。android:background,你必须确保为空出 backgroundTint(无论是app:backgroundTint="@null"或app:backgroundTint="@empty"),以避免自定义背景没有得到着色。android:background默认值MaterialShapeDrawable。一些特征如笔画、形状外观、波纹没有设置(因为它们与 相关MaterialShapeDrawable)。您必须为他们提供您的自定义背景。不要使用该
android:background属性。MaterialButton管理其自己的背景可绘制对象,并且设置新的背景意味着MaterialButton不能再保证它引入的新属性将正常运行。如果更改了默认背景,MaterialButton则不能保证行为明确。
修改按钮背景的唯一受支持方法是为app:backgroundTint属性设置颜色值。不幸的是,这里也不支持渐变。您只能使用简单的颜色值或ColorStateList。
因此,没有受支持的方法将渐变背景与MaterialButton一起使用。
androidx.appcompat.widget.AppCompatButton如果您正在使用 Material Theme 来应用背景渐变,请使用它,因为 Material Button 尚不提供对它的支持。
对于背景渐变,在drawable文件夹中创建 3 个新资源:
selector_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:drawable="@drawable/btn_gradient_inactive" android:state_enabled="false" />
<item android:drawable="@drawable/btn_gradient_active" />
</selector>
Run Code Online (Sandbox Code Playgroud)
btn_gradient_inactive.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#01579B"
android:endColor="#1A237E"
android:angle="0" />
<corners android:radius="5dp"/> </shape>
Run Code Online (Sandbox Code Playgroud)
btn_gradient_active.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#2196F3"
android:endColor="#3F51B5"
android:angle="0" />
<corners android:radius="5dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
在您的styles.xml:
<style name="BtnStyle" parent="Widget.AppCompat.Button.Borderless">
<item name="android:layout_marginStart">35dp</item>
<item name="android:layout_marginEnd">35dp</item>
<item name="android:background">@drawable/selector_btn_gradient</item>
<item name="android:textColor">@color/selector_text_color</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">14sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在您的layout.xml文件中:
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/my_btn_id"
style="@style/BtnStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
小智 5
我找到了这个解决方案。
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_gradient_example"
app:backgroundTint="@null" />
Run Code Online (Sandbox Code Playgroud)
选择器_梯度_示例
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/banana_yellow">
<item android:state_enabled="true">
<ripple android:color="@color/banana_yellow">
<item>
<shape android:shape="rectangle">
<gradient android:angle="135" android:endColor="@color/banana_yellow" android:startColor="@color/main_yellow" />
</shape>
</item>
</ripple>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<gradient android:angle="315" android:endColor="#ede0be" android:startColor="#e0bf7e" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1040 次 |
| 最近记录: |