編輯:關於 Android 程式設計
本實例功能雖簡單,但透過擴展,可以在自訂標題中做出菜單導航等實用功能。要實現自訂視窗標題,需三個步驟:提供自訂標題的介面、將它套用給 Activity 視窗、把系統預設主題改為自訂主題。
/res/layout/custom_title.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/rectangle">
<Button android:id="@+id/infoAtMeTextView"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@我" />
<Button android:id="@+id/infoCommentTextView"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="評論" />
<Button android:id="@+id/infoPrivateMsgTextView"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="私信" />
</LinearLayout>
/drawable/rectangle.xml,繪製一個帶漸層的長方形:
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:angle="270"
android:endColor="#1DC9CD"
android:startColor="#A2E0FB" />
<padding android:left="2dp" android:top="2dp"
android:right="2dp" android:bottom="2dp" />
</shape>
public class CustomTitleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 指定使用自訂標題
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}
}
運行後會發現自訂 layout 無法填滿整個標題欄。查看原始碼可知,系統為 Activity 標題預設套用了 screen_title.xml,其中 ?android:attr/windowTitleSize(標題高度)、windowTitleBackgroundStyle(背景樣式)、windowContentOverlay(前景)等屬性會限制自訂版面。因此需透過自訂主題覆寫這些屬性。
/res/values/style.xml:
<resources>
<!-- 繼承系統預設樣式 -->
<style name="customTheme" parent="android:Theme">
<!-- 標題前景設為透明 -->
<item name="android:windowContentOverlay">@drawable/nocolor</item>
<!-- 標題高度設為 44dp -->
<item name="android:windowTitleSize">44dp</item>
<!-- 標題背景樣式 -->
<item name="android:windowTitleBackgroundStyle">@style/customBg</item>
</style>
<style name="customBg">
<item name="android:background">@drawable/rectangle</item>
</style>
</resources>
其中 @drawable/nocolor 定義於 strings.xml:
<drawable name="nocolor">#00000000</drawable>
在 AndroidManifest.xml 中為 Activity 指定 android:theme="@style/customTheme":
<activity android:name=".CustomTitleActivity"
android:theme="@style/customTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
完成以上步驟,自訂的標題列(含三個可點擊的導航按鈕)就能填滿並正常顯示。以此為基礎,可進一步加入圖示、選單或狀態切換,打造專屬的頂部導航列。
Android 內存優化(三):MAT 與 DDMS 內存分析工具的使用
一、用 Eclipse Heap 分析內存洩露 Android 開發中難免遇到內存洩露問題。先大致說明一下基本概念:官方對內存洩露的解釋是——用動態存儲分配函數動態開闢的
安卓基礎學習(一):開發環境建置與第一個 HelloWorld
在動手建置 Android 開發環境之前,先花點時間認識一下 Android 本身:它以 Java 為主要開發語言,核心基於 Linux,屬於自由且開放原始碼的作業系統,
Android 中 SurfaceTexture、TextureView、GLSurfaceView 与 SurfaceView 的区别与联系详解
SurfaceView、GLSurfaceView、SurfaceTexture 和 TextureView 是 Android 里名字容易混淆、关系又相当密切的几个类。
自定義 ViewPager:打造多變的圖片切換動畫效果
效果展示 傳統 ViewPager 切換較單調,透過自定義可以在滑動過程中加入縮放、平移、淡入淡出等動畫。 實作分析 關鍵是在頁面滾動時取得當前 View 與目標 Vi