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



例如,要创建一个按钮,其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>

最后,按钮.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,以及几个ShapeDrawables

<?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>

相关内容

  • 没有找到相关文章

最新更新