在3个方面在drawable xml的android视图上创建边框?

use*_*572 13 android styles xml-drawable shapedrawable

我想为我的Android按钮制作一个drawable,定义为drawable.我发现我可以通过使用一个矩形来设置所有边框,但是当我想要三面时,我有点卡住了.我希望例如打开顶部或底部.

谁能告诉我怎么做?

Shu*_*ayu 27

尝试这样做,虽然我在其他帖子上看到了它

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

<item>
<shape android:shape="rectangle" >
    <solid android:color="#FFFFFF" />
    <padding
        //android:bottom="10dp"  Remove this to avoid seeing the bottom border 
        android:left="10dp"
        android:right="10dp"
        //android:top="10dp"  Remove this to avoid seeing the top border
    />

    <corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

    <solid android:color="#666666" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

/sf/answers/709340411/

  • 这"有效",但它不是最好的解决方案,因为它创造了透支 (2认同)

小智 6

如果有人在接受的答案中遇到有关填充生效的问题,请尝试查看:https://stackoverflow.com/a/11006931.该解决方案使用标签上的位置属性来实现相同的效果.

在创建用于ActionBar的drawable时,我在使用接受的答案时遇到了麻烦,并且链接的方法对我有用.


小智 6

这可能对你有帮助。

I XML 三边的边框

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="0dp" android:right="0dp" android:top="0dp" android:bottom="-5dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />

            <corners android:radius="0dp" />
            <stroke android:width="3dp" android:color="#000000" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)