【问题标题】:set Background Image and xml Resource设置背景图片和xml资源
【发布时间】:2011-07-07 11:24:28
【问题描述】:

我使用下面的代码来圆角RelativeLayout。我将其另存为 mybackground.xmldrawable 文件夹中。
圆角效果很好,但问题是我还想添加一个透明图像作为我的 RelativeLayout 的背景。我怎样才能实现这两件事?如何同时为 RelativeLayout 使用图像和可绘制 xml(用于圆角)...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" android:padding="10dp">
   <corners android:bottomRightRadius="30dp"
            android:bottomLeftRadius="30dp"
            android:topLeftRadius="30dp"
            android:topRightRadius="30dp" />
</shape>

【问题讨论】:

    标签: android android-xml repeat


    【解决方案1】:

    使用Layer List和Item标签设置Image,使用solid标签设置颜色#AA000000表示透明,如下图

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="rectangle" android:padding="10dp">
          <solid android:color="#AA000000"/>
          <corners android:bottomRightRadius="30dp"
                   android:bottomLeftRadius="30dp" 
                   android:topLeftRadius="30dp"
                   android:topRightRadius="30dp" />
        </shape>
      </item>
      <item>
        <bitmap android:src="@drawable/yourfilename"/>
      </item>
    </layer-list>
    

    【讨论】:

    • 您好,非常感谢以上所有 cmets 指导我的工作正常。 . . . .
    • 太棒了,我用这种方法为片段提供了一个圆角..谢谢
    • @AmithGC:谢谢老哥的鼓励和支持,:)
    【解决方案2】:

    您可以使用Layer-List

    会是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
        <shape android:shape="rectangle" android:padding="10dp">
        <corners android:bottomRightRadius="30dp"
                 android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
                 android:topRightRadius="30dp" />
        </shape>
      </item>
      <item><!-- your transparent image --></item>
     </layer-list>
    

    【讨论】:

    • 并将这个新的drawable设置为RelativeLayout的背景
    【解决方案3】:

    这个有效。如果您不圆角您的图片,您将无法看到圆角。有朋友在之前的回答中谈到了这个。他们应该将图像四舍五入才能看到圆形效果。

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/listviewback2">
            <shape android:shape="rectangle" android:padding="10dp">
                <corners
                    android:bottomRightRadius="35dp"
                    android:bottomLeftRadius="35dp"
                    android:topLeftRadius="35dp"
                    android:topRightRadius="35dp"/>
            </shape>
        </item>
    </layer-list>
    

    【讨论】:

      【解决方案4】:

      试试这样的:

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          <item> 
              <shape xmlns:android="http://schemas.android.com/apk/res/android"
                  android:shape="rectangle" android:padding="10dp">
                  <corners android:bottomRightRadius="30dp"
                      android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
                      android:topRightRadius="30dp" />
              </shape>
          </item>
         <item>
             <bitmap android:src="@drawable/background"/>
         </item>
      </layer-list>
      

      【讨论】:

        猜你喜欢
        • 2013-12-02
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-21
        • 2015-05-09
        • 2012-03-07
        相关资源
        最近更新 更多