2012-04-13 53 views

回答

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... 
0

我不完全確定你在問什麼。但是,如果您只是在尋找默認的按鈕背景,只需使用

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

填充可能需要手動完成。

這個link給出了Android資源的列表。

+0

這是行不通的。資源不公開。這是我得到::錯誤:錯誤:資源不公開。 (在'background'處,值爲'@android:drawable/btn_default_normal')。 – michael 2012-04-13 16:29:22

0

爲了澄清,爲什麼要繼承相對佈局以創建自定義按鈕?如果你只需要改變按鈕的外觀,你可以通過指定一個可選擇的drawable來完成。

如果有需要繼承RelativeLayout,那麼你的代碼會更多一些,你需要處理你的按鈕的狀態。如果這樣做正確,你可以附上一個可以按照blessenm提到的背景選擇器。

0

正如我得到你。您可能喜歡像this那樣實施。另請參閱this例如。

有按鈕的樣式定義,您可以在任何時候使用該樣式到按鈕。

希望這就是你想要的。 如果沒有,然後讓我知道。

享受。 :)

相關問題