編輯:關於 Android 程式設計
以前我沒有寫筆記的習慣,總覺得看過一次就記得住,最近才發現學過的東西大量流失。於是現在只要碰到新知識就寫下來。這次做了一個 Mono for Android 的專案,改動幅度比上一個點餐系統大得多;上回因為沒留筆記,很多細節都忘了,所以這回把過程整理出來,一方面方便自己複習,一方面也跟大家交流,歡迎指教。
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="https://schemas.android.com/apk/res/android"> <style name="CustomWindowTitleBackground"> <item name="android:background">#565656</item> </style> <style name="test" parent="android:Theme"> <item name="android:windowTitleSize">40dp</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style> </resources>
這段做了兩件事:把標題列背景改成深灰色 #565656,並把標題列高度設為 40dp。若日後要改成真正的 ActionBar,只要換掉 parent 主題並改用對應的 item 即可。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f5f5f5" > <LinearLayout android:id="@+id/mytitlebar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/Logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="彩雲" android:textSize="32sp" /> </LinearLayout> </LinearLayout>
注意:原本的範例把內層 LinearLayout 的高度寫成 fill_parent,這樣標題列會把整個畫面吃光。實務上應該改成 wrap_content,如上。
先用 Attribute 指定主題:
[Activity(Label = "Cloud", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/test")]
接著在程式碼中換掉系統預設的標題列:
// 宣告使用自訂的 titleBar this.RequestWindowFeature(WindowFeatures.CustomTitle); // 注意:SetContentView 必須排在 RequestWindowFeature 之後 SetContentView(Resource.Layout.Main); // 設定自訂 titleBar Window.SetFeatureInt(WindowFeatures.CustomTitle, Resource.Layout.myTitle);
順序是這段程式最容易出錯的地方:RequestWindowFeature 一定要在 SetContentView 之前呼叫,否則會拋出 AndroidRuntimeException: requestFeature() must be called before adding content。另外範例中還出現過 SetTitle(Resource.Layout.myTitle) 這種寫法,那是誤用——SetTitle 收的是字串,不是版面資源 id,請以上面的 Window.SetFeatureInt 為準。
這樣一來,標題列上就同時有了 App 圖示與文字,而樣式(高度、底色)則由 mystyle.xml 統一控管,後續要換風格只改資源檔就好。
Android 自動化測試入門:用 AS 2.2 的 Record Espresso 錄製 UI 測試
讓開發好的 App 自己跑一遍測試流程,是不是很酷?那麼,透過在螢幕上點幾下就自動生成測試代碼,是不是更酷?本文借助 Android Studio 2.2 的 Recor
Android Notification 入門:建立、更新與自訂提醒
通知的作用 通知是 Android 與使用者互動的重要方式,適合用於即時訊息、推送、下載進度、日曆提醒等場景。好的通知能在不打斷使用者當前操作的情況下傳遞重要資訊;但若濫
解決 Virtualbox 虛擬機下 Android x86 4.0 無法聯網的問題
自從 Android x86 問世後,越來越多人開始在虛擬機上安裝體驗,但安裝過程並不簡單,網上因而出現各種教學。其中,Virtualbox 虛擬機下的 Android
Android Studio 開發環境貼身配置指南:從主題到快捷鍵一次搞定
界面設置 默認的 Android Studio 採用灰色界面,如果你偏愛炫酷的黑色風格,可以這樣調整。 Settings-->Appearance-->The