Android 中的自定义按钮:当我从 xml 读取背景时如何获取边框/边缘/框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:51:47  来源:igfitidea点击:

Custom Buttons in Android: How to get border/edge/frame when I read the background from xml?

android

提问by Anna

Using Android Shapes in xml I have defined a gradient which I use as the background for a button.

在 xml 中使用 Android Shapes 我定义了一个渐变,用作按钮的背景。

This all works nice, but there's no edge surrounding the button. I would like it to look similar to the normal Android button but I need more flexibility to control the color and look.

这一切都很好,但按钮周围没有边缘。我希望它看起来类似于普通的 Android 按钮,但我需要更大的灵活性来控制颜色和外观。

The shape is defined as follows:

形状定义如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#FFFFFF" 
        android:endColor="#00FF00"
        android:angle="270" />
    <corners android:radius="3dp" />
    <stroke android:width="5px" color="#000000" />
</shape>

I would expect the border to be set in the xml. Why doesn't "stroke" fix it? Stroke doesn't seem to do anything. I checked the Android Developer spec, but couldn't find the answer there: http://developer.android.com/guide/topics/resources/drawable-resource.html

我希望在 xml 中设置边框。为什么“中风”不解决它?中风似乎没有任何作用。我检查了 Android Developer 规范,但在那里找不到答案:http: //developer.android.com/guide/topics/resources/drawable-resource.html

I have also looked through all the properties of the Android Button, but as expected there's no such parameter, probably since it's built into the normal Android button. Btw, I checked ImageButton properties too.

我还查看了 Android 按钮的所有属性,但正如预期的那样,没有这样的参数,可能是因为它内置于普通的 Android 按钮中。顺便说一句,我也检查了 ImageButton 属性。

Can someone please help? I know there's the alternative to make an image with proper edges and use an ImageButton, but there really should be a way to fix this programmatically.

有人可以帮忙吗?我知道有一种替代方法可以制作具有适当边缘的图像并使用 ImageButton,但确实应该有一种方法可以以编程方式解决此问题。

Thanks! Anna

谢谢!安娜

回答by Steve Haley

I had this problem a while ago. While I don't quite remember why I made each decision, the way I solved it was to use a a shape layer-list. This lets you stack one shape on top of another. For example, the following XML creates a shape with a solid black outline 2px wide, with a 'grey to white to grey' gradient across the middle:

不久前我遇到了这个问题。虽然我不太记得我为什么做出每个决定,但我解决它的方法是使用形状图层列表。这使您可以将一个形状堆叠在另一个形状上。例如,下面的 XML 创建了一个形状,它有一个 2px 宽的纯黑色轮廓,中间有一个“灰色到白色到灰色”的渐变:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <padding android:left="1dp"
                android:top="1dp"
                android:right="1dp"
                android:bottom="1dp"/>
            <solid android:color="#FF000000"/>
            <corners android:radius="3dp"/>
        </shape>
    </item>

    <item>
        <shape>
            <padding android:left="2dp"
                android:top="2dp"
                android:right="2dp"
                android:bottom="2dp"/>
            <gradient android:startColor="#FFB0B0B0"
                android:centerColor="#FFFFFFFF"
                android:endColor="#FFB0B0B0"
                android:angle="315"/>
        </shape>
    </item>
</layer-list>

If you want to be able to change that color dynamically at runtime, then things get a lotmessier. Again, the details of why I had to do things a certain way are hazy, but I ended up having to create a custom view class which contained a custom ShapeDrawable. I started off looking at the examples from the ApiDemos app which comes with the SDK - it's a very good resource.

如果你希望能够动态地改变颜色在运行时,事情得到了很多混乱。同样,为什么我必须以某种方式做事的细节是模糊的,但我最终不得不创建一个包含自定义 ShapeDrawable 的自定义视图类。我开始查看 SDK 附带的 ApiDemos 应用程序中的示例 - 这是一个非常好的资源。

EDIT:Another reason your stroke might not be appearing is that you forgot the android:before the color="...." bit.

编辑:您的笔划可能不会出现的另一个原因是您忘记了android:之前的 color="...." 位。

回答by praveenb

I've had the same problem, what i observed is stroke is not applying to button as border at design time but at run time i see the border.

我遇到了同样的问题,我观察到的是笔画在设计时没有应用于按钮作为边框,但在运行时我看到了边框。

I jst used the following same code

我刚刚使用了以下相同的代码

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="@color/black" />
    <stroke  android:width="1px" android:color="@color/red" />

</shape>

as steve hanley said abvoe you miss the android:for the color attribute.

正如史蒂夫汉利所说,你错过了android:对于颜色属性。

Hope this helps somebody....

希望这可以帮助某人....

回答by Dusty

Probably much to late, but you have to add an extra ff before the color.

可能太晚了,但是您必须在颜色之前添加额外的 ff。

<stroke android:width="5px" color="#ff000000" />

<stroke android:width="5px" color="#ff000000" />

Greets

问候

回答by Srikanth Goli

Use ImageButton

使用图像按钮

   <ImageButton
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:src="@drawable/left_arrow"
        android:background="@drawable/button_selector"
        android:gravity="center"/>

Use drawable selector to ImageButton and define your properties

使用可绘制选择器到 ImageButton 并定义您的属性

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="@color/startColor"
            android:endColor="@color/endColor"
            android:angle="270" />
        <stroke
            android:width="0.5dp"
            android:color="@color/borderColor" />
        <corners
            android:topLeftRadius="5dp"
            android:bottomRightRadius="5dp"/>
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <gradient
            android:startColor="@color/startColor"
            android:endColor="@color/endColor"
            android:angle="270" />
        <stroke
            android:width="0.5dp"
            android:color="@color/borderColor" />
        <corners
            android:topLeftRadius="5dp"
            android:bottomRightRadius="5dp"/>
    </shape>
</item>

<item>        
    <shape>
        <gradient
            android:startColor="@color/startColor"
            android:endColor="@color/endColor"
            android:angle="270" />
        <stroke
            android:width="0.5dp"
            android:color="@color/borderColor" />
        <corners
            android:topLeftRadius="5dp"
            android:bottomRightRadius="5dp"/>
    </shape>
</item>    

回答by shruthi

<item android:state_enabled="false" android:drawable="@drawable/button_disabled" />

<item android:state_focused="true" android:drawable="@drawable/button_highlighted"/>
<item android:state_pressed="true" android:drawable="@drawable/button_highlighted"/>

<item>
    <shape>
        <gradient android:startColor="#fdfdfd" android:endColor="#f0f0f0" android:angle="270" />
        <stroke android:width="1dp" android:color="#56390a" />
        <corners android:radius="3dp" />
        <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
    </shape>
</item>

Adding stroke color #56390awill solve the problem.

添加stroke color #56390a将解决问题。