【问题标题】:How can I add an Image in my custom shape xml如何在我的自定义形状 xml 中添加图像
【发布时间】:2014-09-19 12:38:04
【问题描述】:

我正在编写一个 XML 选择器

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

我只希望 state1 是透明的。所以图像被视为 - 是

状态1

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="270"
    android:endColor="#00000000"
    android:startColor="#00000000" />

现在我想让 state2 稍微遮蔽一下我已经完成的图片。我的问题是,除了阴影渐变之外,我还希望在中心出现一个图标。我不知道如何通过xml添加这个

状态2

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="270"
    android:endColor="#80000000"
    android:startColor="#80000000" />

【问题讨论】:

    标签: android xml android-layout android-ui


    【解决方案1】:

    你可以通过使用图层列表来做到这一点:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
     <item>
      <shape android:shape="rectangle" >
          <gradient
        android:angle="270"
        android:endColor="#80000000"
        android:startColor="#80000000" />
      </shape>
     </item>
     <item>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:tileMode="repeat" android:gravity="center" />
     </item>
    </layer-list>
    

    将以上内容另存为@drawable/image_state2。

    更多信息: Resize bitmap inside a drawable layer-list

    【讨论】:

    • 太完美了!!你今天救了我,非常感谢你:) 你知道我怎样才能让它居中吗?如果可能的话设置它的尺寸?
    • 尝试将重力设置为中心 android:gravity="center" 用于位图
    • 是的,我自己找到了最后一个,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    相关资源
    最近更新 更多