简体   繁体   English

Android创建可绘制XML,图像为圆角

[英]Android Create Drawable XML with Image rounded top corners

I'm trying to create an Android XML Drawable that I can use as the background for one of my LinearLayouts. 我正在尝试创建一个Android XML Drawable,我可以将其用作我的一个LinearLayouts的背景。 I need the background to have an image, with the top left and top right corners rounded with a 10dp radius. 我需要背景才能有一个图像,左上角和右上角的圆角半径为10dp。 I've been trying with the code below, but just can not seem to get it working. 我一直在尝试下面的代码,但似乎无法让它工作。 Is this possible? 这可能吗? Any help is greatly appreciated!! 任何帮助是极大的赞赏!!

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

Try using px instead of dp: 尝试使用px而不是dp:

<corners android:topLeftRadius="10px"
         android:topRightRadius="10px">                
</corners>

EDIT: 编辑:

This is the complete XML that I am using to round edges in one of my elements: 这是我用来在我的一个元素中舍入边缘的完整XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <solid android:color="#99000000" ></solid>
   <corners android:radius="8px"></corners> 
   <stroke  android:width="0dp" android:color="#A4C2E0"></stroke>  
</shape>

EDIT 2: 编辑2:

I would try this. 我会尝试这个。 Put the image and it's layout (not rounded) in a new XML and place it in a drawable folder. 将图像及其布局(不是圆角)放在新的XML中,并将其放在可绘制的文件夹中。 Let's say it's named linearlayout_bg.xml... In your main layout, create a new LinearLayout and apply this following attribute: 假设它名为linearlayout_bg.xml ...在主布局中,创建一个新的LinearLayout并应用以下属性:

android:background="@drawable/linearlayout_bg"

And then use your <corners code. 然后使用你的<corners代码。 Maybe that'll work? 也许那会有用吗?

Try adding a solid element to the shape... 尝试在形状中添加实体元素......

my sample: 我的样本:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#0f0" />
            <corners android:radius="30dp" />

        </shape>
    </item>

    <item>
        <bitmap android:src="@drawable/ic_launcher" />
    </item>
</layer-list>

Just tried this and it seems to work perfectly 试过这个,看起来效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM