我從這裏所說的那些不同的方法,它是真正的工作好吧,所以我想分享它。
我正在使用樣式創建一個自定義按鈕,圖像位於左側,文本位於中右側。只需按照以下4個「簡單步驟」進行操作:
I.使用至少3個不同的PNG文件和您擁有的工具創建9個修補程序:/YOUR_OWN_PATH/android-sdk-mac_x86/tools/./draw9patch。在這之後,你應該有:
button_normal.9.png,button_focused.9.png和button_pressed.9.png
然後下載或創建一個24x24的PNG圖標。
ic_your_icon.png
保存所有在你的Android項目的繪製/文件夾。
二,在drawable /文件夾下的項目中創建一個名爲button_selector.xml的XML文件。國家應該是這樣的:
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_focused" />
<item android:drawable="@drawable/button_normal" />
三,轉至值/文件夾並打開或創建styles.xml文件,並創建以下XML代碼:
<style name="ButtonNormalText" parent="@android:style/Widget.Button">
<item name="android:textColor" >@color/black</item>
<item name="android:textSize" >12dip</item>
<item name="android:textStyle" >bold</item>
<item name="android:height" >44dip</item>
<item name="android:background" >@drawable/button_selector</item>
<item name="android:focusable" >true</item>
<item name="android:clickable" >true</item>
</style>
<style name="ButtonNormalTextWithIcon" parent="ButtonNormalText">
<item name="android:drawableLeft" >@drawable/ic_your_icon</item>
</style>
ButtonNormalTextWithIcon是「子風格」,因爲它正在擴大ButtonNormalText(以下簡稱「父樣式」)。
請注意,將ButtonNormalTextWithIcon樣式中的drawableLeft更改爲drawableRight,drawableTop或drawableBottom,可以將該圖標放置在與文本相關的其他位置。四,參考文獻轉至佈局/文件夾,你有你的XML的用戶界面,並轉到要應用的風格,使它看起來像這樣的按鈕:
<Button android:id="@+id/buttonSubmit"
android:text="@string/button_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/ButtonNormalTextWithIcon" ></Button>
而且......瞧!你在左側拿到了一個圖片。
對我來說,這是更好的方式來做到這一點!因爲按照這種方式進行操作,您可以根據要顯示的圖標單獨管理按鈕的文本大小,並根據Android UI準則使用樣式爲具有不同圖標的多個按鈕使用相同的背景可繪製。
您還可以爲您的應用程序創建一個主題,併爲其添加「父風格」,以便所有按鈕看起來都一樣,並將「子風格」與圖標一起應用到您需要的地方。
使用9Patch,智能的解決方案 – AndroSco 2015-07-25 04:41:14
如果你能喜@Charuක請檢查該http://stackoverflow.com/questions/42968587/android-email-client-receiver-email- id-empty-in-android-parse – 2017-03-26 10:26:31