Как установить градиент в качестве фона для textview?

Я пытаюсь поместить градиентный фон в textView. Но когда я запускаю его, я получаю сообщение об ошибке :

java.lang.RuntimeException: Unable to start activity  ComponentInfo{cresol.deal/cresol.deal.MainActivity}:  android.view.InflateException: Binary XML file line #41: Error inflating class  TextView
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                             at android.app.ActivityThread.access0(ActivityThread.java:155)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                             at android.os.Looper.loop(Looper.java:135)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                          Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class TextView
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                                                             at cresol.deal.MainActivity.onCreate(MainActivity.java:15)
                                                             at android.app.Activity.performCreate(Activity.java:6010)
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                             at android.app.ActivityThread.access0(ActivityThread.java:155) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                             at android.os.Looper.loop(Looper.java:135) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                                                          Caused by: android.content.res.Resources$NotFoundException: File res/drawable/main_pink_gradient.xml from drawable resource ID #0x7f020057
                                                             at android.content.res.Resources.loadDrawableForCookie(Resources.java:2482)
                                                             at android.content.res.Resources.loadDrawable(Resources.java:2384)
                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                                                             at android.view.View.<init>(View.java:3742)
                                                             at android.widget.TextView.<init>(TextView.java:637)
                                                             at android.widget.TextView.<init>(TextView.java:632)
                                                             at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
                                                             at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
                                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                                                             at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)
                                                             at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)
                                                             at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                             at cresol.deal.MainActivity.onCreate(MainActivity.java:15) 
                                                             at android.app.Activity.performCreate(Activity.java:6010) 
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) 
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                             at android.app.ActivityThread.access0(ActivityThread.java:155) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                             at android.os.Looper.loop(Looper.java:135) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                                                          Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5<gradient> tag requires 'angle' attribute to be a multiple of 45
                                                             at android.graphics.drawable.GradientDrawable.updateGradientDrawableGradient(GradientDrawable.java:1354)
                                                             at android.graphics.drawable.GradientDrawable.inflateChildElements(GradientDrawable.java:1176)
                                                             at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:1019)
                                                             at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1128)
                                                             at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:199)
                                                             at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127)
                                                             at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1128)
                                                             at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1032)
                                                             at android.content.res.Resources.loadDrawableForCookie(Resources.java:2472)
                                                             at android.content.res.Resources.loadDrawable(Resources.java:2384) 
                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
                                                             at android.view.View.<init>(View.java:3742) 
                                                             at android.widget.TextView.<init>(TextView.java:637) 
                                                             at android.widget.TextView.<init>(TextView.java:632) 
                                                             at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62) 
                                                             at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58) 
                                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080) 
                                                             at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) 
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) 
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                             at cresol.deal.MainActivity.onCreate(MainActivity.java:15) 
                                                             at android.app.Activity.performCreate(Activity.java:6010) 
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) 
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                             at android.app.ActivityThread.access0(ActivityThread.java:155) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                             at android.os.Looper.loop(Looper.java:135) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

Я сделал XML-файл со следующим кодом :

<?xml version="1.0" encoding="utf-8"?>
<selector >
<item>
  <shape>
    <gradient
        android_startColor="@color/mainPinkStart"
        android_endColor="@color/mainPinkEnd"
        android_angle="125" />
  </shape>
</item>
</selector>

и мой основной код действия :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 

android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingBottom="@dimen/activity_vertical_margin"
android_paddingTop="@dimen/activity_vertical_margin"
tools_context="cresol.deal.MainActivity">
<LinearLayout
    android_layout_width="match_parent"
    android_layout_height="200dp"
    android_orientation="vertical">
<HorizontalScrollView
    android_layout_width="wrap_content"
    android_layout_height="wrap_content"
    android_overScrollMode="never"
    android_scrollbars="none"
    android_id="@+id/horizontalScrollView">

    <LinearLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_layout_marginLeft="15dp"
        android_layout_marginRight="15dp"
        android_orientation="horizontal"

        android_layout_alignParentTop="true"
        android_layout_alignParentStart="true">

        <ImageButton
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_src="@drawable/home_unpressed"
            android_background="@null"
           android_id="@+id/main_imageButton_home"
            />

    </LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<TextView
    android_layout_width="100dp"
    android_layout_height="100dp"
    android_background="@drawable/main_pink_gradient"/>

<android.support.v7.widget.CardView
    
    android_layout_width="match_parent"
    android_layout_height="100dp"
    card_view_cardCornerRadius="20dp"
    card_view_cardElevation="8dp"
    card_view_cardMaxElevation="10dp"
    card_view_cardBackgroundColor="#fff"
    android_background="#a9a9a9"
    android_layout_marginLeft="20dp"
    android_layout_marginRight="20dp"
    android_layout_alignParentBottom="true"
    android_layout_centerHorizontal="true"
    android_layout_marginBottom="30dp">

</android.support.v7.widget.CardView>
</RelativeLayout>

Мой файл цветов :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="mainPinkStart">#F44195</color>
<color name="mainPinkEnd">#EE82FA</color>
</resources>

Я не могу найти ни одной проблемы, где это происходит ….
Я также попытался использовать этот XML-файл :

<shape

android_shape="rectangle">
<gradient
    android_startColor="@color/mainPinkStart"
    android_endColor="@color/mainPinkEnd"
    android_angle="125" />
</shape>

но это также не сработало, и я получил ту же ошибку.

2 ответа

  1. android:angle="125"
    

    125 не кратно 45, использовать несколько 45

    угол поддержки 45,90,135,180,225,270,315,360

  2. градиентный тег требует, чтобы атрибут ‘angle’ был кратным 45

    <shape
    
    android_shape="rectangle">
    <gradient
    android_startColor="@color/mainPinkStart"
    android_endColor="@color/mainPinkEnd"
    android_angle="90" />
    </shape>