用到Button,誰都會想到網頁那裡面各式各樣的Button,那麼你是否也想設計出那種效果呢?呵呵,答案是肯定的,android很強大。但是,今天剛邁出第一步就發現了一個頭疼的問題。什麼事都是從簡到繁再到簡的,就像Button的設計也一樣。想設計出很多不同的很拉風的Button肯定第一步就是給Button設定背景,我也是這麼做的,可是設定背景為顏色後Button變成了Textview一樣,並且高度跟文字一樣,這肯定不是我想要的結果啦,然後百度得到了結果:先說再貼連結。android Button 已經預設了有個實現,內部是通過State List(想知道,看官方文檔,悄悄的告訴你們,這個篇章讀完就可以設計自己的Button了哦,文檔目錄為Dev Guid------Drawable Resources)實現的,現在我們用android:drawable="@color/red" 把原來的樣式給覆蓋了,所以就出現了這種悲劇。解決方案:1.自己定義一個xml類型drawable資源,eg:
res/values/colors.xml----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>
res/drawable/button.xml ---------------------------- ①
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/blue" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@color/red" /> <!-- focused -->
<item android:drawable="@color/green" /> <!-- default -->
</selector>
使用
<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:text="show"
android:background="@drawable/button"
/>
然後設定Button的height,就可以有自己的顯示樣式了。
第二種解決方案是把①處的drawable改成圖片,那樣也能有固定的高度了。
說了這些,應給大家一個參考了:http://blog.csdn.net/zhanghw0917/article/details/5630538
做完上述操作,你就覺得滿足了嗎?這不應當是我們的終點,而是起點。
現在,就可以實現一些複雜的Button了,不多講,我也是看的,就貼連結了:
http://blog.csdn.net/xjanker2/article/details/6222690