編輯:關於 Android 程式設計
自定義標題欄在 Android 應用裡非常常見, left 放返回鍵、中間放標題、right 放操作按鈕,幾乎是列表詳情頁的標準配置。這個功能本身不難,但涉及主題、窗口特性、布局三處配合,任何一環出錯都會報一個讓人摸不着頭腦的錯誤。以下記錄完整實現過程與踩到的坑。
Theme.Holo、Theme.Light 等,程序會一直報錯:you cannot combine custom title with other feature titles。super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
<resources>
<style name="WindowTitleBackground">
<item name="android:background">@color/blue</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">60dp</item>
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
</resources>
RelativeLayout 對齊自定義標題中的各個組件。這是關鍵技巧:因為返回鍵與操作按鈕的文案長度不固定,用 LinearLayout 無法保證標題居中,而 RelativeLayout 通過 layout_toLeftOf/layout_toRightOf 讓標題自動佔據中間剩餘空間,配合 layout_centerHorizontal 即可穩定居中。
package com.gloomyfish.titledemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal"
android:layout_height="fill_parent" >
<Button
android:id="@+id/header_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:text="返回"
android:textColor="#000000"/>
<TextView
android:id="@+id/header_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/header_left_btn"
android:layout_toLeftOf="@+id/header_right_btn"
android:text="My Title Bar"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:singleLine="true" />
<Button
android:id="@+id/header_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:text="圖片"
android:textColor="#000000"/>
</RelativeLayout>
最後在 AndroidManifest.xml 中為該 Activity 掛上自定義主題:
android:theme="@style/MyTheme"
同時要刪除 IDE 默認生成的 AppTheme 引用,否則主題衝突會導致持續報錯。
回顧整個過程,三個高頻問題依次是:主題裡殘留了 Theme.Holo/Theme.Light 系列導致「不能與其他 title feature 組合」的異常;requestWindowFeature 的位置寫到了 setContentView 之後;以及 Manifest 中主題沒有真正生效。
需要說明的是,這套基於 FEATURE_CUSTOM_TITLE 的方案屬於早期寫法。在現代 Android 開發中,官方推薦直接在布局裡使用 Toolbar(或 MaterialToolbar):它本質上就是一個普通 ViewGroup,返回鍵、標題、菜單項都能通過 XML 或 menu 資源聲明,居中標題只需設置 app:titleCentered="true",配合 setSupportActionBar() 還能獲得統一的 ActionBar 能力,完全不需要折騰窗口特性與主題衝突。上述內容適合用於理解窗口裝飾機制或維護老項目。
Android 自訂 View:打造會旋轉並帶漣漪的甜甜圈進度環(二)
先看最終效果。 靜態: 動態: 一、建立骨架 public class DoughnutProgress extends View { } 二、常數、變數與初
PigeonCall 手機網絡電話 App 的整體架構剖析
1. 概述 PigeonCall(中文名「飛鴿電話」)是一款運行於 Android 平台的 VoIP 網絡電話應用,工作範圍限定在局域網內,可與同網段中安裝了本應用的其他
Android 高仿微信朋友圈:雙擊放大與滑動查看圖片
前不久參與開發了一款旅行 App,包含實時聊天與動態評論兩大塊。聊天部分用戶量不大時用 Socket 就能解決,量級上萬可以考慮 smack + openfire,或者
Android 鬧鐘功能的完整實作方案與踩坑記錄
先前有人在後台問有沒有設定鬧鐘的範例,當時答應要寫,一直騰不出時間,最近又有人催,於是決定認真做一個。鬧鐘這件事看起來簡單,實際坑不少。我把常用邏輯抽成了一個可直接呼叫的