【问题标题】:Can we create multiple shapes in one xml file and point them within this single file?我们可以在一个 xml 文件中创建多个形状并将它们指向这个单个文件吗?
【发布时间】:2019-11-27 20:53:45
【问题描述】:

例如要创建一个按钮,其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 文件中创建多个形状并将它们在同一个文件中指向其他东西(如果需要) - 例如参见上面的来源?所以在这种情况下,我们不会为每个形状创建额外的文件。

提前致谢!!!

【问题讨论】:

    标签: android android-studio android-drawable android-shape android-shapedrawable


    【解决方案1】:

    嵌套作品:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2016-09-21
      • 1970-01-01
      • 2012-09-17
      • 2013-04-13
      相关资源
      最近更新 更多