android background設定顏色後就成textview了

來源:互聯網
上載者:User

 用到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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.