2013-08-29 19 views
41

我想通過使用XML樣式如何使用XML樣式來創建Android的自定義按鈕

enter image description here

這只是一個例子,我想做出這種按鈕[相同的背景&文本]顏色寫一些其他的文本,如:關於我

不過我使用由設計師在Photoshop

<ImageButton 
     android:id="@+id/imageButton5" 
     android:contentDescription="AboutUs" 
     android:layout_width="wrap_content" 
     android:layout_marginTop="8dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/view_pager" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/aboutus" /> 
創建的按鈕

注:我需要這種按鈕的每一個大小和形狀

我不想用我的Android應用我要讓使用XML僅

+3

我們這樣的圖像,並設置爲您的imageView的背景。 –

回答

60

你有沒有嘗試創建的背景 任何按鈕的形狀?

請查看下面的表單:

下面是從您的圖片按鈕分離的圖像。現在

enter image description here

,把在你的ImageButton爲Android:SRC 「源」,例如:

android:src="@drawable/twitter" 

現在,剛剛創建的ImageButton的形狀有一個黑色的着色背景。

android:background="@drawable/button_shape" 

和button_shape是繪製資源的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke 
     android:width="1dp" 
     android:color="#505050"/> 
    <corners 
     android:radius="7dp" /> 

    <padding 
     android:left="1dp" 
     android:right="1dp" 
     android:top="1dp" 
     android:bottom="1dp"/> 

    <solid android:color="#505050"/> 

</shape> 

剛剛嘗試這個來實現它。您可能需要根據您的要求更改顏色值。

讓我知道它是否無效。從「阿德里安Santalla」寫菜譜

5
<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 

    <solid 
     android:color="#ffffffff"/> 

    <size 
     android:width="@dimen/shape_circle_width" 
     android:height="@dimen/shape_circle_height"/> 
</shape> 
任何圖像

1.增加這在繪製

2.設置爲背景,以您的按鈕

+0

我知道如何使用形狀,但我需要在我的代碼中實現相同的背景和文字顏色 – Sneha

+1

使用[this](http://www.colorzilla.com)從圖像中派生顏色 – Gru

16

看一看Styled Button它一定會幫助你。 有很多例子請在INTERNET上搜索。

如:風格

<style name="Widget.Button" parent="android:Widget"> 
    <item name="android:background">@drawable/red_dot</item> 
</style> 

,你可以用它代替red_dot您選擇

red_dot:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" > 

    <solid android:color="#f00"/> 
    <size android:width="55dip" 
     android:height="55dip"/> 
</shape> 

按鈕:

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="49dp" 
     style="@style/Widget.Button" 
     android:text="Button" /> 
+0

是的,我知道但我需要在我的代碼中實現相同的背景和文字顏色 – Sneha

+0

@Saneha你的意思是你在問題中給出的圖像的確切顏色? – Siddhesh

+0

@Sneha你有互聯網連接使用它。你可以找到該背景顏色的顏色代碼。使用Gru的答案來創建具有該顏色的圖像。搜索符合您的要求的字體並應用於該按鈕即可。 – Siddhesh

61

複製粘貼上androidcookbook.com: https://www.androidcookbook.com/Recipe.seam?recipeId=3307

1.創建一個代表按鈕狀態

創建一個XML到繪製所謂的XML文件'button.xml' 來命名的按鈕狀態:

<?xml version="1.0" encoding="utf-8"?> 
 

 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 
    <item 
 
     android:state_enabled="false" 
 
     android:drawable="@drawable/button_disabled" /> 
 
    <item 
 
    \t android:state_pressed="true" 
 
    \t android:state_enabled="true" 
 
     android:drawable="@drawable/button_pressed" /> 
 
    <item 
 
    \t android:state_focused="true" 
 
    \t android:state_enabled="true" 
 
     android:drawable="@drawable/button_focused" /> 
 
    <item 
 
    \t android:state_enabled="true" 
 
     android:drawable="@drawable/button_enabled" /> 
 
</selector>

2.創建表示每個按鈕狀態

創建四個按鈕狀態中的一個XML文件的XML文件。所有這些應該在drawables文件夾下。讓我們按照button.xml文件中設置的名稱。

button_enabled.xml:

<?xml version="1.0" encoding="utf-8"?> 
 

 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
 
    <gradient 
 
     android:startColor="#00CCFF" 
 
     android:centerColor="#0000CC" 
 
     android:endColor="#00CCFF" 
 
     android:angle="90"/> 
 
    <padding android:left="7dp" 
 
     android:top="7dp" 
 
     android:right="7dp" 
 
     android:bottom="7dp" /> 
 
    <stroke 
 
     android:width="2dip" 
 
     android:color="#FFFFFF" /> 
 
    <corners android:radius= "8dp" /> 
 
</shape>

button_focused.xml:

<?xml version="1.0" encoding="utf-8"?> 
 

 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
 
    <gradient 
 
     android:startColor="#F7D358" 
 
     android:centerColor="#DF7401" 
 
     android:endColor="#F7D358" 
 
     android:angle="90"/> 
 
    <padding android:left="7dp" 
 
     android:top="7dp" 
 
     android:right="7dp" 
 
     android:bottom="7dp" /> 
 
    <stroke 
 
     android:width="2dip" 
 
     android:color="#FFFFFF" /> 
 
    <corners android:radius= "8dp" /> 
 
</shape>

button_pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
 
    <gradient 
 
     android:startColor="#0000CC" 
 
     android:centerColor="#00CCFF" 
 
     android:endColor="#0000CC" 
 
     android:angle="90"/> 
 
    <padding android:left="7dp" 
 
     android:top="7dp" 
 
     android:right="7dp" 
 
     android:bottom="7dp" /> 
 
    <stroke 
 
     android:width="2dip" 
 
     android:color="#FFFFFF" /> 
 
    <corners android:radius= "8dp" /> 
 
</shape>

button_disabled.xml:

<?xml version="1.0" encoding="utf-8"?> 
 

 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
 
    <gradient 
 
     android:startColor="#F2F2F2" 
 
     android:centerColor="#A4A4A4" 
 
     android:endColor="#F2F2F2" 
 
     android:angle="90"/> 
 
    <padding android:left="7dp" 
 
     android:top="7dp" 
 
     android:right="7dp" 
 
     android:bottom="7dp" /> 
 
    <stroke 
 
     android:width="2dip" 
 
     android:color="#FFFFFF" /> 
 
    <corners android:radius= "8dp" /> 
 
</shape>

3.創建一個代表的按鈕樣式的XML文件

創建上述文件後,就可以創建應用程序按鈕樣式了。現在,您需要創建一個名爲styles.xml的新XML文件(如果您還沒有它),可以將更多自定義樣式包含到de values目錄中。

此文件將包含應用程序的新按鈕樣式。您需要在其中設置新的按鈕樣式功能。請注意,這些功能之一(新樣式的背景)應該通過引用第一步中創建的按鈕(button.xml)drawable來設置。要引用新的按鈕樣式,我們使用name屬性。

下面的例子示出了styles.xml文件的內容:

<resources> 
 
    <style name="button" parent="@android:style/Widget.Button"> 
 
     <item name="android:gravity">center_vertical|center_horizontal</item> 
 
     <item name="android:textColor">#FFFFFFFF</item> 
 
     <item name="android:shadowColor">#FF000000</item> 
 
     <item name="android:shadowDx">0</item> 
 
     <item name="android:shadowDy">-1</item> 
 
     <item name="android:shadowRadius">0.2</item> 
 
     <item name="android:textSize">16dip</item> 
 
     <item name="android:textStyle">bold</item> 
 
     <item name="android:background">@drawable/button</item> 
 
     <item name="android:focusable">true</item> 
 
     <item name="android:clickable">true</item> 
 
    </style> 
 
</resources>

4。使用您自己的自定義應用程序主題創建XML

最後,您需要重寫默認的Android按鈕樣式。爲此,您需要創建一個名爲themes.xml的新XML文件(如果您還沒有),可以將它們放入values目錄並覆蓋默認的Android按鈕樣式。

下面的例子顯示的themes.xml的內容:

<resources> 
 
    <style name="YourApplicationTheme" parent="android:style/Theme.NoTitleBar"> 
 
     <item name="android:buttonStyle">@style/button</item> 
 
    </style> 
 
</resources>

希望你們能有同樣的運氣,因爲我有這個,我一直在尋找的自定義按鈕。請享用。

+0

好的,得到了​​一個更好的答案,肯定會幫助其他人。關於回答一個老話題,我覺得,當我正在尋找它時,其他人也可能會這樣做。更完整的是一個話題,更快的開發人員可以找到他們的問題的解決方案,至少我是這樣想的。不管怎麼說,還是要謝謝你。 – Gramowski

+0

很好的答案,但任何想法爲什麼填充選項被忽略? – user3690202

+0

只是供參考 - 兄弟,你可能想改變鏈接的http到http - 因爲該網站不再使用安全協議。 –