【问题标题】:How can I apply android's ImageButton style to my own button?如何将 android 的 ImageButton 样式应用于我自己的按钮?
【发布时间】:2012-04-25 11:26:49
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    填充可能需要手动完成。

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

    【讨论】:

    • 这不起作用。资源不是公开的。这就是我得到的::错误:错误:资源不公开。 (在“背景”,值为“@android:drawable/btn_default_normal”)。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      为按钮定义了样式,您可以随时在按钮中使用该样式。

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

      享受。 :)

      【讨论】:

        【解决方案4】:
        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... 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-25
          • 1970-01-01
          • 2013-01-18
          • 2015-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多