[英]Background Drawable is not getting applied to the Button
This is the background for a login button (login_button_bk):
这是登录按钮 (login_button_bk) 的背景:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="27dp" />
<gradient
android:angle="45"
android:centerX="35%"
android:endColor="#79EBFD"
android:startColor="#408bff"
android:type="linear" />
<size
android:width="182dp"
android:height="54dp" />
</shape>
This is the button Im applying the background to:
这是我将背景应用到的按钮:
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/loginViewsMargin"
android:background="@drawable/login_button_bk"
android:text="Login" />
This is the styles.xml:
这是 styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Still, the button is just Black without the drawable applied.
不过,按钮只是黑色,没有应用可绘制对象。 How to fix it?
如何解决?
To get the drawable gradient in your button you must change the attribute from Buton to androidx.appcompat.widget.AppCompatButton要在按钮中获得可绘制渐变,您必须将属性从Buton更改为androidx.appcompat.widget.AppCompatButton
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/loginViewsMargin"
android:background="@drawable/login_button_bk"
android:text="Login" />
Also using the above method for creating a drawable won't have ripple effect in your button to add that use this code同样使用上述方法创建可绘制对象不会在您添加使用此代码的按钮中产生连锁反应
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/white">
<item>
<shape
android:shape="rectangle">
<corners android:radius="27dp" />
<gradient
android:angle="45"
android:centerX="35%"
android:endColor="#79EBFD"
android:startColor="#408bff"
android:type="linear" />
<size
android:width="182dp"
android:height="54dp" />
</shape>
</item>
</ripple>
Just Change your theme as below只需更改您的主题如下
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.