【问题标题】:Android Create Drawable XML with Image rounded top cornersAndroid 创建带有圆角顶角的可绘制 XML
【发布时间】:2014-07-30 17:38:45
【问题描述】:

我正在尝试创建一个 Android XML Drawable,我可以将它用作我的一个 LinearLayouts 的背景。我需要背景有一个图像,左上角和右上角以 10dp 半径圆角。我一直在尝试使用下面的代码,但似乎无法让它工作。这可能吗?非常感谢任何帮助!

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

【问题讨论】:

    标签: android xml android-layout android-xml


    【解决方案1】:

    尝试使用 px 代替 dp:

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

    编辑:

    这是我用来在我的一个元素中圆边的完整 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>
    

    编辑 2:

    我会试试这个。将图像及其布局(非四舍五入)放入新的 XML 中,并将其放置在可绘制文件夹中。假设它被命名为 linearlayout_bg.xml... 在您的主布局中,创建一个新的 LinearLayout 并应用以下属性:

    android:background="@drawable/linearlayout_bg"

    然后使用您的&lt;corners 代码。也许这会起作用?

    【讨论】:

    • 但在高密度设备上,您几乎看不到圆角,因为像素几乎不占任何空间。
    • 有趣。我的一些应用程序有一些圆润的边缘。我将使用我正在使用的完整 XML 代码编辑我的答案 - 也许有人会看到我没有看到的东西。
    • @IAmTheSquidward 虽然您提供的代码确实创建了圆角,但我需要将它们应用于包含图像的 XML Drawable,而不仅仅是纯色。
    • @Phil - 是的,我认为这就是问题所在。我想我有个主意。我现在正在编辑我的答案。
    【解决方案2】:

    尝试在形状中添加实体元素...

    我的样本:

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

    刚刚试了一下,感觉效果不错

    【讨论】:

    • 刚刚尝试使用您的确切代码,但用我的可绘制图像替换了位图 src。没有运气。根本没有圆角。
    猜你喜欢
    • 2012-02-14
    • 2014-09-02
    • 2012-08-21
    • 1970-01-01
    • 2013-04-01
    • 2015-07-26
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多