2

您好,我尝试创建一个具有多渐变形状的可绘制 XML 文件。

我想要这样的东西,左上角是红色,右上角是黄色,底部是蓝色,如 3 个区域。 在此处输入图像描述

我能找到的最接近的事情是这里的其他问题。 多渐变形状 用户已通过使用修复它layer-list。我已经尝试过了,但这不起作用,我能理解的是我会这样做但不起作用。(这是button_real_world.xml文件)

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
>
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle"
        >
            <stroke android:width="4dp" android:color="#FFFF7700" /> 
            <padding android:left="7dp" android:top="7dp" 
                     android:right="7dp" android:bottom="7dp" /> 
            <corners android:radius="15dp" /> 

            <gradient
                android:angle="45"
                android:startColor="#FF63a34a"
                android:endColor="#FF477b36"
                android:type="linear"
                />    
        </shape>
    </item>
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle"
        >
            <stroke android:width="4dp" android:color="#FFFF7700" /> 
            <padding android:left="7dp" android:top="7dp" 
                     android:right="7dp" android:bottom="7dp" /> 
            <corners android:radius="15dp" /> 
            <gradient
                android:angle="250"
                android:startColor="#FF63a34a"
                android:endColor="#ff477b36"
                android:type="linear"
                />    
        </shape>            
    </item>
</layer-list>

但这只会给我一个运行时错误,(在模拟器中运行,Windows)

W/dalvikvm( 1075): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 1075): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1075): android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
E/AndroidRuntime( 1075):        at android.view.LayoutInflater.createView(LayoutInflater.java:513)
E/AndroidRuntime( 1075):        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime( 1075):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
E/AndroidRuntime( 1075):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
E/AndroidRuntime( 1075):        at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 1075):        at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 1075):        at se.evigfilosofi.transportmanagement.util.ui.AdvancedArrayAdapter.createViewFromResource(AdvancedArrayAdapter.java:224)
E/AndroidRuntime( 1075):        at se.evigfilosofi.transportmanagement.util.ui.AdvancedArrayAdapter.getView(AdvancedArrayAdapter.java:213)
E/AndroidRuntime( 1075):        at android.widget.AbsListView.obtainView(AbsListView.java:1274)
E/AndroidRuntime( 1075):        at android.widget.ListView.makeAndAddView(ListView.java:1668)
E/AndroidRuntime( 1075):        at android.widget.ListView.fillDown(ListView.java:637)
E/AndroidRuntime( 1075):        at android.widget.ListView.fillFromTop(ListView.java:694)
E/AndroidRuntime( 1075):        at android.widget.ListView.layoutChildren(ListView.java:1521)
E/AndroidRuntime( 1075):        at android.widget.AbsListView.onLayout(AbsListView.java:1113)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.onLayout(LinearLayout.java:920)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1388)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.onLayout(LinearLayout.java:920)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime( 1075):        at android.view.View.layout(View.java:6830)
E/AndroidRuntime( 1075):        at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
E/AndroidRuntime( 1075):        at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
E/AndroidRuntime( 1075):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1075):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1075):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 1075):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1075):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1075):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 1075):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 1075):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1075): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1075):        at android.widget.LinearLayout.<init>(LinearLayout.java:92)
E/AndroidRuntime( 1075):        at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 1075):        at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
E/AndroidRuntime( 1075):        at android.view.LayoutInflater.createView(LayoutInflater.java:500)
E/AndroidRuntime( 1075):        ... 52 more
E/AndroidRuntime( 1075): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/button_real_world.xml from drawable resource ID #0x7f020002
E/AndroidRuntime( 1075):        at android.content.res.Resources.loadDrawable(Resources.java:1693)
E/AndroidRuntime( 1075):        at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
E/AndroidRuntime( 1075):        at android.view.View.<init>(View.java:1
4

1 回答 1

1

尝试使用这个。提示:设置角度时,请使用 45 的倍数。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <gradient
                android:type="radial"
                android:gradientRadius="150%"
                android:centerX="50%"
                android:centerY="100%"
                android:startColor="#78ffd6" />
        </shape>
    </item>
    <item>
        <shape  android:shape="rectangle" >
             <gradient
                android:type="radial"
                android:gradientRadius="125%"
                android:centerX="10%"
                android:centerY="0%"
                android:startColor="#f64f59"
                android:centerColor="#7df64f59"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
             <gradient
                 android:type="radial"
                 android:gradientRadius="100%"
                 android:centerX="100%"
                 android:centerY="0%"
                 android:startColor="#F3F9A7"
                 android:centerColor="#7df3f9a7"/>
        </shape>
    </item>
</layer-list>
于 2018-07-15T03:38:00.407 回答