Android Activity定製需要的Title

Activity界面默認的Title是隻有文字描述的,當我們想要做成類似微信中Tilte行帶有導航和多功能效果的時候,我們就需要自己去定義需要的佈局來加載它。

 

上次我們講到了Activity的全屏顯示和去掉Title,說白了就是改變Window窗口features

    /** Flag for the "options panel" feature.  This is enabled by default. */
    public static final int FEATURE_OPTIONS_PANEL = 0;
    /** Flag for the "no title" feature, turning off the title at the top
     *  of the screen. */
    public static final int FEATURE_NO_TITLE = 1;
    /** Flag for the progress indicator feature */
    public static final int FEATURE_PROGRESS = 2;
    /** Flag for having an icon on the left side of the title bar */
    public static final int FEATURE_LEFT_ICON = 3;
    /** Flag for having an icon on the right side of the title bar */
    public static final int FEATURE_RIGHT_ICON = 4;
    /** Flag for indeterminate progress */
    public static final int FEATURE_INDETERMINATE_PROGRESS = 5;
    /** Flag for the context menu.  This is enabled by default. */
    public static final int FEATURE_CONTEXT_MENU = 6;
    /** Flag for custom title. You cannot combine this feature with other title features. */
    public static final int FEATURE_CUSTOM_TITLE = 7;
    /**
     * Flag for enabling the Action Bar.
     * This is enabled by default for some devices. The Action Bar
     * replaces the title bar and provides an alternate location
     * for an on-screen menu button on some devices.
     */
    public static final int FEATURE_ACTION_BAR = 8;
    /**
     * Flag for requesting an Action Bar that overlays window content.
     * Normally an Action Bar will sit in the space above window content, but if this
     * feature is requested along with {@link #FEATURE_ACTION_BAR} it will be layered over
     * the window content itself. This is useful if you would like your app to have more control
     * over how the Action Bar is displayed, such as letting application content scroll beneath
     * an Action Bar with a transparent background or otherwise displaying a transparent/translucent
     * Action Bar over application content.
     *
     * <p>This mode is especially useful with {@link View#SYSTEM_UI_FLAG_FULLSCREEN
     * View.SYSTEM_UI_FLAG_FULLSCREEN}, which allows you to seamlessly hide the
     * action bar in conjunction with other screen decorations.
     *
     * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, when an
     * ActionBar is in this mode it will adjust the insets provided to
     * {@link View#fitSystemWindows(android.graphics.Rect) View.fitSystemWindows(Rect)}
     * to include the content covered by the action bar, so you can do layout within
     * that space.
     */
    public static final int FEATURE_ACTION_BAR_OVERLAY = 9;
    /**
     * Flag for specifying the behavior of action modes when an Action Bar is not present.
     * If overlay is enabled, the action mode UI will be allowed to cover existing window content.
     */
    public static final int FEATURE_ACTION_MODE_OVERLAY = 10;

通過上面的源碼我們可以看到,如果我要定製自己的Title,我們需要採用Window.FEATURE_CUSTOM_TITLE這個特徵來實現。

需要注意的是,Window.FEATURE_CUSTOM_TITLE這個特徵不能其他特徵一起使用,還有就是必須在setContentView()方法之前使用(這個是所有Window特徵改變的必須條件),不然無效


首先是佈局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:shadowColor="#000986"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="1"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher" />

</RelativeLayout>

我只加入了2張圖片,大家可以按照自己的需要來添加元素


下面就上代碼了

getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.main);

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

這樣就實現了自定義Title的效果,當時,當我們運行在android sdk 3.0以上的時候會出現

報如下錯誤:android.util.AndroidRuntimeException: You cannot combine custom titles with other title features


原因在於,3.0以上的系統默認使用了Window.FEATURE_ACTION_BAR,我們需要使用自己的樣式來修改它

首先考慮到不同版本,建立vlaues-v11和values-v14兩個文件夾滿足,3.0以上和4.0以上

看樣式style.xml

改變背景色修改android:windowTitleBackgroundStyle的值,改變標題欄高度則修改 android:windowTitleSize的值

去掉windowActionBar特徵

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
        <item name="android:windowActionBar">false</item>
        <item name="android:windowTitleSize">50dip</item> 
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    </style>
    
    <style name="CustomWindowTitleBackground"> 
        <item name="android:background">@drawable/stripes</item> 
    </style> 

</resources>

接着再修改AndroidManifest.xml文件,找到要自定義標題欄的Activity,添加上android:theme值,       

<activity android:name=".MainActivity" android:theme="@style/activityTitlebar"> 

這樣就解決了問題




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章