简体   繁体   中英

Android: How to place an edge around the button

So I want to make my button have white edges/border with a black background, is there a simple solution to do this in my xml?

Yup, Make a new XML file in your res/drawable directory. and then create a shape drawable via XML: Here's an example for a 3-px rounded black rectangle with a 2-px white border:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <corners 
        android:radius="3"
        />
    <stroke
        android:width="2"
        android:color="#FFFFFFFF"
        />
    <solid
        android:color="#FF000000"
        />
</shape>

Then just set this drawable as the background of your button, for example:

<Button
    android:background="@drawable/my_xml_file"
    />

The developer site has a great reference on creating Shape Drawables via XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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