【问题标题】:Is there a way to define android drawable containing a stack of images, using just xml?有没有办法只使用xml来定义包含一堆图像的android drawable?
【发布时间】:2012-06-18 13:07:12
【问题描述】:

我想在我的布局 XML 中使用一组 2 个图像层作为背景。

我猜我能做到:

  1. 使用代码,可能是通过扩展 ImageView/Canvas/Drawable,这是我试图避免的。
  2. 使用布局 XML,具有 2 个嵌套布局,每个图层图像作为背景。或者一个带有 2 个 ImageView 的 FrameLayout。

但是,我想知道是否有另一种方法可以使用 XML 定义可绘制对象(就像我使用 XML 定义动画集一样)。

类似这样的:

mylayout.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/2_image_layer_drawable" >

2_image_layer_drawable.xml [虚构]

   <Set>
       <drawable src="@image_layer_back"/>
       <drawable src="@image_layer_front"/>
   </Set>

有吗?

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    使用layer-list 和两个Bitmap(带有这些图像)作为孩子的drawable。 示例:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <bitmap android:src="@drawable/ic_launcher" />
        </item>
        <item>
            <bitmap android:src="@drawable/ic_launcher" />
        </item>
    
    </layer-list>
    

    【讨论】:

    • 太棒了! layer-list 正是我想要的。我感觉有这样的东西可用,但找不到。谢谢@Luksprog!
    • @dostiharise 我还添加了一个带有layer-list 的小示例。
    • 谢谢,我希望我能投票给答案,但声誉不够。 :-)
    【解决方案2】:

    你在说这个吗:

    .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="@image_layer_back" />
        <item android:state_focused="true" android:drawable="@image_layer_back" />
        <item android:state_selected="true" android:drawable="@image_layer_back" />
        <item android:drawable="@image_layer_front" />
    </selector>
    

    【讨论】:

    • 不,我以前使用过选择器作为按钮。我认为 layer-list 是我正在寻找的。 :-) 谢谢@VineetShukla
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多