2

我创建了自己的按钮,它继承了android 的RelativeLayout。我的问题是如何将 android 的按钮样式(即背景可绘制、填充)应用于我自己的按钮?

谢谢你。

4

4 回答 4

2
in your drawable folder - create this xml say custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" >
            <shape>
                <solid
                    android:color="#E77A26" />
                <stroke
                    android:width="1dp"
                    android:color="#E77A26" />
                <corners
                    android:radius="3dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
        <item>
            <shape>
                <gradient
                    android:startColor="#70c656"
                    android:endColor="#53933f"
                    android:angle="270" />
                <stroke
                    android:width="1dp"
                    android:color="#53933f" />
                <corners
                    android:radius="4dp" />
                <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    </selector>

And in your main layout - 

     <Button
            android:id="@+id/connect"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_button"
            android:layout_marginRight="10dp"
            android:text="@string/connect" />

if you want to put styles to your text in that button - add a style resource in your strings.xml - like below

    <style name="buttonText">
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:textColor">#ffffff</item>
            <item name="android:gravity">center</item>
            <item name="android:textSize">15dp</item>
            <item name="android:textStyle">bold</item>
            <item name="android:shadowColor">#000000</item>
            <item name="android:shadowDx">1</item>
            <item name="android:shadowDy">1</item>
            <item name="android:shadowRadius">2</item>
        </style>

You can use this style in your button as below, 

    style="@style/buttonText"

Thats it... 
于 2012-04-13T04:41:31.513 回答
0

正如我得到你一样。你可能喜欢这样实现。另请参阅示例。

Button 有样式定义,您可以随时在按钮上使用该样式。

希望这就是你想要的。如果没有,请告诉我。

享受。:)

于 2012-04-13T04:30:15.653 回答
0

我不完全确定你在问什么。但是,如果您只是在寻找默认按钮背景,只需使用

android:background="@android:drawable/btn_default_normal"

填充可能需要手动完成。

这个链接给出了 android 资源的列表。

于 2012-04-13T04:24:44.650 回答
0

为了澄清起见,为什么要继承相对布局来创建自定义按钮?如果您只需要更改按钮的外观,则可以通过指定一个可绘制的选择器来完成。

如果有继承RelativeLayout 的要求,那么您的代码会多一点,您需要处理按钮的状态。如果做得正确,您可以像 blessenm 提到的那样附加一个可绘制的背景选择器。

于 2012-04-13T04:25:52.133 回答