相关疑难解决方法(0)

从 Android Studio 4.1 开始,Android 背景可绘制对象在按钮中不起作用

我发现从 Android Studio 4.1 开始,我无法Button通过在 a 上设置颜色来更改 a 的背景颜色android:background,只是没有效果。自定义Drawable也不起作用。

我的背景Drawable

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="1.5dp"
        android:color="@android:color/black" />

    <solid
        android:color="@android:color/white" />

    <corners
        android:radius="8dp" />

</shape>
Run Code Online (Sandbox Code Playgroud)

我的Button

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add To Cart"
    android:background="@drawable/background3"/>
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

android android-button android-drawable android-styles material-design

23
推荐指数
4
解决办法
1万
查看次数

如何通过xml向android中的按钮添加渐变?

我无法弄清楚为什么这段代码不起作用.有人可以帮忙吗?我试图让按钮使用名为'greenstart'和'greenend'的自定义颜色.颜色已在res/value/string.xml文件中创建.我已经查看了类似的问题,但要么他们没有得到答复,不清楚,要么解决方案不起作用.提前致谢.

XML文件示例:

<Button
   android:id="@+id/mycollection"
   android:layout_width="match_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1" >

   <Gradient
      android:angle="270"
      android:endColor="@color/greenstart"
      android:startColor="@color/greenend" >
   </Gradient>
</Button>
Run Code Online (Sandbox Code Playgroud)

错误日志:

08-28 21:47:20.574: E/AndroidRuntime(761): FATAL EXCEPTION: main
08-28 21:47:20.574: E/AndroidRuntime(761): java.lang.RuntimeException: Unable to start activity ComponentInfo{My.Taste.App/My.Taste.App.MyTasteActivity}: android.view.InflateException: Binary XML file line #50: Error inflating class Gradient
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.os.Looper.loop(Looper.java:137)
08-28 21:47:20.574: E/AndroidRuntime(761):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-28 21:47:20.574: E/AndroidRuntime(761):  at …
Run Code Online (Sandbox Code Playgroud)

xml user-interface android colors button

16
推荐指数
3
解决办法
4万
查看次数