我们可以在一个 xml 文件中创建多个形状并将它们指向这个文件吗?

Can we create multiple shapes in one xml file and point them within this single file?

例如创建一个按钮,其button_normal状态显示一些不同的风格和button_pressed状态显示一些不同的风格,我们创建三个文件:

button_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#0084FF" />
</shape>

button_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF19F4" />
</shape>

最后,button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/button_pressed" />
    <item android:drawable="@drawable/button_normal" />
</selector>

如您所见,在 button.xml 文件中,我们指向 button_normal.xmlbutton_pressed.xml。好的,这很正常。

实际对价:

现在的问题是,是否可以在 button.xml 中添加 button_normal.xmlbutton_pressed.xml 的源,并指向这两个形状(button_normalbutton_pressed) 在同一文件 button.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/button_pressed" />
    <item android:drawable="@drawable/button_normal" />
</selector>

<shape android:shape="rectangle">
    <solid android:color="#0084FF" />
    <corners android:radius="3dp" />
</shape>

<shape android:shape="rectangle">
    <solid android:color="#FF19F4" />
</shape>

问题的总结是,是否可以在一个 xml 文件中创建多个形状,并将它们在同一个文件中指向其他对象(如果需要)——例如,请参阅上面的源代码?所以在那种情况下我们不会为每个形状创建额外的文件。

提前致谢!!!

嵌套作品:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#0084FF" />
            <corners android:radius="3dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FF19F4" />
        </shape>
    </item>
</selector>

您可以嵌套很多 XML 可绘制资源。这是来自 Android SDK (seekbar_track_material.xml) 的一个复杂示例,混合了一个 LayerListDrawable、一个 ScaleDrawable、一个 StateListDrawable 和几个 [=16] =]:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@id/background"
          android:gravity="center_vertical|fill_horizontal">
        <shape android:shape="rectangle"
               android:tint="?attr/colorProgressBackgroundNormal">
            <corners android:radius="?attr/progressBarCornerRadius" />
            <size android:height="@dimen/seekbar_track_background_height_material" />
            <solid android:color="@color/white_disabled_material" />
        </shape>
    </item>
    <item android:id="@id/secondaryProgress"
          android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <selector>
                <item android:state_enabled="false"
                      android:drawable="@color/transparent" />
                <item>
                    <shape android:shape="rectangle"
                           android:tint="?attr/colorControlActivated">
                        <corners android:radius="?attr/progressBarCornerRadius" />
                        <size android:height="@dimen/seekbar_track_progress_height_material" />
                        <solid android:color="@color/white_disabled_material" />
                    </shape>
                </item>
            </selector>
        </scale>
    </item>
    <item android:id="@id/progress"
          android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <selector>
                <item android:state_enabled="false"
                      android:drawable="@color/transparent" />
                <item>
                    <shape android:shape="rectangle"
                           android:tint="?attr/colorControlActivated">
                        <corners android:radius="?attr/progressBarCornerRadius" />
                        <size android:height="@dimen/seekbar_track_progress_height_material" />
                        <solid android:color="@color/white" />
                    </shape>
                </item>
            </selector>
        </scale>
    </item>
</layer-list>