简体   繁体   English

具有顶部和底部边距的自定义按钮

[英]Custom button with top and bottom margin

I need to create a layout like below. 我需要创建如下的布局。

安卓

And I worked out how to create a this layout: 我想出了如何创建此布局的方法:

安卓

How can I add top and bottom margins? 如何添加顶部和底部边距? And button background color? 和按钮背景色? I made the button transparent by using: 我通过使用以下方法使按钮透明:

android:background="@android:color/transparent"

Now how can I make button background color to light blue. 现在如何使按钮背景色变为浅蓝色。

If I use custom button layout, how can I do that? 如果我使用自定义按钮布局,该怎么做? I checked lots of Stackoverflow questions, but all of them concern gradient, color change on click, etc. 我检查了很多Stackoverflow问题,但所有这些问题都涉及渐变,单击时颜色变化等。

Thank you! 谢谢!

You don't want to use the transparent background color ... instead set it to the blue color you want. 您不想使用透明的背景色...而是将其设置为所需的蓝色。 Then use setAlpha to make it partially transparent: 然后使用setAlpha使其部分透明:

MyButton.getBackground().setAlpha(50);

To set both the border and transparent background, I think you'll have to make an XML drawable and define as the button's background: 要设置边框和透明背景,我认为您必须使XML可绘制并定义为按钮的背景:

android:background="@drawable/mybuttonbackground"

Then the drawable resource should be like this (/res/drawable/mybuttonbackground.xml): 然后可绘制资源应如下所示:(/res/drawable/mybuttonbackground.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

   <item android:top="1dp" android:bottom="1dp"> 
        <shape ="rectangle">
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
            <solid android:color="#FFFFFF" />
        </shape>
   </item>
</layer-list>

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

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