編輯:關於 Android 程式設計
Android 預設的標題列相當樸素,想放圖示、狀態文字或自訂按鈕,就得自己動手。整個過程可以拆成四件事:寫一個標題版面、畫一個漸層背景、在 Activity 中掛上它,最後(若要求完美貼合)覆寫系統樣式。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/hello_world"
android:textColor="#FF00FF" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="添加" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充色為漸變色,只需 startColor 與 endColor -->
<gradient
android:angle="270"
android:startColor="#A2E0FB"
android:endColor="#1DC9CD" />
<!-- 定義內間距 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
</shape>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
package com.example.customertitle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
// 自定義標題
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1. 宣告要使用自訂標題(必須在 setContentView 之前)
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
// 2. 把自訂標題的 XML 掛到視窗上
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
}
public void add(View v) {
Toast.makeText(this, "按鈕被點擊", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
這裡有個順序陷阱:requestWindowFeature() 一定要在 setContentView() 之前呼叫,否則會直接丟出例外。
部署之後你會看到自訂標題並沒有完全覆蓋,左右各留了一小塊空白。原因是系統的標題容器自帶樣式。查看 Android 原始碼中 platforms/android-8/data/res/layout/screen_custom_title.xml:
<!-- 系統窗口的界面文件 -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
<FrameLayout android:id="@android:id/title_container"
android:layout_width="match_parent"
android:layout_height="?android:attr/windowTitleSize" >
</FrameLayout>
<FrameLayout android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>
其中用到了三個屬性,定義在 data/res/values/themes.xml:
<style name="Theme">
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
<item name="windowTitleSize">25dip</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
</style>
而 WindowTitleBackground 又在 data/res/values/styles.xml 中:
<style name="WindowTitleBackground">
<item name="android:background">@android:drawable/title_bar</item>
</style>
<resources xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 定義一個樣式,覆蓋原有主題 -->
<style name="myTheme" parent="android:Theme">
<item name="android:windowContentOverlay">@drawable/color</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/textViewBg</item>
</style>
<style name="textViewBg">
<item name="android:background">@drawable/rectangle</item>
</style>
</resources>
再把這個主題掛到 AndroidManifest.xml 的 <application> 標籤上即可。顏色與字串資源的定義:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CustomerTitle</string>
<string name="action_settings">Settings</string>
<string name="hello_world">自定義標題</string>
<drawable name="color">#00000000</drawable>
</resources>
把 windowContentOverlay 換成全透明、放大 windowTitleSize、並將背景換成自己的漸層,標題列就能完整貼合邊界了。
自定義控制項:復刻舊版優酷的三級旋轉菜單
效果預覽: 舊版優酷那個「點一下、面板翻轉展開」的側邊菜單, pernah 讓不少人想自己動手做一個。拆解之後其實不難,核心只有兩件事:三層重疊的 Relative
MTK 6735(Android 6.0)平台按鍵燈實現全解析
一、按鍵燈是什麼 最近剛好把按鍵燈相關的流程調了一遍,趁還有印象整理成筆記。顧名思義,按鍵燈就是一種 LED:按下按鍵時亮燈,若一段時間沒有操作則自動熄滅。 值得先建立的
Android for Work 實戰總結(下):流程演示與 EMM 配置
Afw 流程演示Device Owner(L 平台)先把設備恢復出廠設置,接著連接可翻牆的 Wi-Fi,直到出現如下引導界面:點擊「Set up work device」
安卓模擬器安裝教學:BlueStacks 下載與安裝圖解
對 BlueStacks 這個名稱,很多人可能還很陌生。平常從手機下載的應用,想在電腦上先試用,但電腦是 Windows 系統,跑不了手機 App——裝一個安卓模擬器就能