android shape圖形最佳化Button效果

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   檔案   

  android shape可以讓我們通過定義xml檔案的方式建立圖形,當然只能實現一些比較簡單的圖形(圓形,矩形,橢圓,線段),但是已經相當不錯了,通過shape建立的圖形作為控制項的背景已經基本可以滿足我的簡單需求了,而且通過shape建立的圖形可以適配各種螢幕。

  下面就用shape定義的圖形來最佳化Button的整體效果。

  定義主布局檔案activity_main.xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     tools:context=".MainActivity" > 6  7     <Button  8         android:layout_height="48dp" 9         android:layout_width="160dp"10         android:background="@drawable/button_selector"11         android:layout_centerInParent="true"12         android:text="按鈕"/>13 </RelativeLayout>

   定義Button的狀態選取器button_selector.xml:

1 <?xml version="1.0" encoding="utf-8"?>2 <selector3   xmlns:android="http://schemas.android.com/apk/res/android">4     <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_shape" />5     <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_shape" />6     <item android:state_pressed="true" android:drawable="@drawable/button_shape_press" />7     <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_shape" />8 </selector>

  定義Button未點擊狀態的背景button_shape.xml:

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3     android:shape="rectangle"> 4     <!--漸層 --> 5     <gradient android:angle="0" android:endColor="#DCDCE1" android:startColor="#DCDCE1" android:centerColor="#DCDCE1"/> 6     <!--實心  --> 7     <solid android:color="#DCDCE1"/> 8     <!--圓角,可定義四個圓角的大小  --> 9     <corners android:radius="3dp" />10     <!--描邊, 可設定邊的寬度顏色 -->11     <stroke android:width="1dp" android:color="#C8C8CD" />12     <!--內邊距-->13     <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>14 </shape>

  定義Button點擊狀態的背景button_shape_press.xml:

1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>2 <shape xmlns:android="http://schemas.android.com/apk/res/android"3     android:shape="rectangle">4     <gradient android:angle="0" android:endColor="#C8C8CD" android:startColor="#C8C8CD" android:centerColor="#C8C8CD"/>5     <solid android:color="#C8C8CD"/>6     <corners android:radius="3dp"/>7     <stroke android:width="1dp" android:color="#C8C8CD" />8     <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>9 </shape>

 

最終效果如:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.