Android介面-標題和按鈕定製-drawable
【原文:http://blog.csdn.net/stalendp/article/details/8087882】
這篇文章記錄了怎麼使用drawable中的StateList來實現自定義標題 以及 按鈕,先看一下效果圖吧:
先介紹一下drawable中的stateList
先看程式碼:
send_button.xml
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" android:drawable="@drawable/btn_pressed" />
- <item android:state_focused="true" android:drawable="@drawable/btn_pressed" />
- <item android:drawable="@drawable/btn_normal" />
- </selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_pressed" />
<item android:state_focused="true" android:drawable="@drawable/btn_pressed" />
<item android:drawable="@drawable/btn_normal" />
</selector>
這段程式碼描述的是“傳送”按鈕,預設情況下,呈現的是btn_normal;當press或者focus的時候,為btn_pressed。兩圖片如下:
而stateList是一種drawable,所以上面的send_button.xml其實在android應用程式看來就是類似一張圖片,不過這張圖片是有狀態的。 作為按鈕,當點選這個控制元件時,它會提供一個友好的相應。
關於stateList,請參考官方文件:http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Tips: 定義中的狀態的匹配是按照順序進行的,匹配到第一個成功就不往下匹配了(這裡我也不太清楚具體的應用場景,反正就是提醒,定義狀態的順序是比較重要的,也可以把這裡的狀態類比有著break的switch語句塊)。狀態的順序很重要,文章有個例子,參考那個例子就可以了,而對於按鈕,運用本文中的例子也可以了。
使用drawable
跟使用圖片是一樣的,直接上程式碼:
- <Button
- android:id="@+id/sendbutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- ...
- android:background="@drawable/send_button"
- android:text="發 送" />
<Button
android:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:background="@drawable/send_button"
android:text="發 送" />
本文中的例子還有一處使用了stateList,就是標題欄中跳轉的導航按鈕。
程式碼如下:
right_selector.xml
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/right_selected" android:state_pressed="true"/>
- <item android:drawable="@drawable/right_normal"/>
- </selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/right_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/right_normal"/>
</selector>
- <ImageButton
- android:id="@+id/about_us_back_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- ...
- android:background="@drawable/right_selector"
- android:src="@drawable/arrow_left" />
<ImageButton
android:id="@+id/about_us_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:background="@drawable/right_selector"
android:src="@drawable/arrow_left" />
設定無標題
預設情況下,android應用是有標題的,而我們這裡定義了標題,就不需要原來的標題了,這裡要使用@android:style/Theme.NoTitleBar這個style。我是在activity中設定的,具體看程式碼:
AndroidManifest.xml
- <activity android:name=".MainActivity"
- android:label="@string/title_activity_main"
- android:theme="@android:style/Theme.NoTitleBar" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
<activity android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
整合layout檔案
我喜歡把功能集中的一些元素放到單一的檔案中,然後使用include引用。
比如,這個應用中,我把title和conent分成了兩個檔案:head.xml和content.xml
然後在activity_main.xml中引用這兩個檔案,具體看程式碼:
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@color/white">
- <include layout="@layout/head"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <include
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="30sp"
- layout="@layout/content" />
- </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<include layout="@layout/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30sp"
layout="@layout/content" />
</LinearLayout>
相關文章
- 窗體(文字框,按鈕,單選按鈕,標籤)
- 製作Gmail式按鈕AI
- android:定製 ListView 的介面AndroidView
- JS實現點選按鈕複製當前介面的URLJS
- Swift更改SearchBar的“No Results”標籤和“Cancel”按鈕Swift
- 分析微信(iOS 版)定製導航欄按鈕的思路iOS
- 安卓Android 按鈕案例安卓Android
- Android開發:定製Activity的標題欄(Titlebar)Android
- 按鈕製作網站收集網站
- PPT製作動感按鈕
- 自定義按鈕 圖片標題位置隨意放置
- Delphi元件開發-在窗體標題欄新增按鈕元件
- iOS_按鈕文字圖片任意佈局,隨意定製iOS
- 設定按鈕的徽章文字
- IOS:修改NavigationController的後退按鈕標題iOSNavigationController
- Android中的Drawable和動畫Android動畫
- 製作百度分享按鈕
- Android ListView中按鈕監聽器設定及優化AndroidView優化
- Fiori Elements List Report table 裡的普通按鈕,Global 按鈕 和 Determining 按鈕
- 設定Android程式圖示和程式標題Android
- 按鈕上面的按鈕 (轉)
- Alert Dialog "Done"按鈕定義.
- WPF Button按鈕設定圓角
- JFrame容器和JButton按鈕
- Qt5.9中QSS(qt Style Sheet)用法之一設定按鈕顏色和背景色(設定按鈕間相互間隔、設定按鈕與周圍邊緣間隔)QT
- Android--按鈕點選事件Android事件
- Android 點選按鈕跳轉Android
- 工具欄上按鈕的繪製 (轉)
- EM按鈕亂碼問題
- wordpress 文章的釋出和修改時定製文章標題
- MFC 捕獲按鈕 按下和抬起 (轉)
- css設定按鈕心跳收縮後,按鈕文字上下抖動,如何解決?CSS
- 按鈕
- LayoutTransiton實現簡單的錄製按鈕
- 11個CSS3按鈕製作教程CSSS3
- HTML input submit和reset按鈕HTMLMIT
- 第 3 章 表格和按鈕
- Oracle EM按鈕亂碼問題Oracle