簡體   English   中英

自定義操作欄/ Android標頭

[英]Custom action bar / header for Android

我正在嘗試創建一個自定義標頭,但是我不知道如何做。 我一直在研究,但是我總是遇到日食所沒有的樣式和問題。 問題是我想要非常簡單的東西。

Android 4.2.2中的屏幕如下所示:

http://imageshack.us/a/img838/8840/89ur.png

我認為這是Android SDK 11等的默認標頭。 我一直在測試它來更改清單android:targetSdkVersion,並且我的手機應用程序的外觀也發生了變化。

問題是不同的Android版本之間的兼容性。 我已經看到該應用程序在不同的設備上運行,結果是:

在Android 4.0.4中

http://imageshack.us/a/img600/6246/rdhl.png

在Android 2.3.6中

http://imageshack.us/a/img836/1599/ezit.png

您會注意到頁眉,甚至顏色和形狀如何變化。 如果我創建了一個自定義標頭,它會在每台設備上以相同的方式出現還是取決於Android版本?

我想為每個設備制作該應用程序的唯一版本,這可能嗎? 如果可能的話,我想要第一張圖片的下一個標題,並且,在紅色矩形內,我想插入一張圖片。 如果我可以將選項菜單(黃色正方形)保留在那里,那就更好了。 如果由於Android版本而無法使用,則可以使用硬件按鈕激活。 提前致謝!

這不切實際,可能不建議這樣做,但是我要做的是使用以下命令將動作欄/頭部完全隱藏在Android Manifest中:

    <activity
        android:name=".My_Activity_Name"
        android:theme="@android:style/Theme.NoTitleBar" />

然后,在我的活動的布局xml文件中,我只是自定義構建一個類似於actionbar / header的標題。 這是我的一個應用程序中的一個示例:

在此處輸入圖片說明

這是完全在xml文件中完成的,但看起來與正確的actionbar / header一樣真實。

編輯

這是我的導航欄的xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@drawable/navbar_topxml"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="55dp"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_back"
            android:onClick="nav_back" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.99"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tab_style"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:paddingLeft="5dp"
            android:text="TITLE"
            android:textColor="#888888"
            android:textStyle="bold"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center" >
            <ImageView
                android:id="@+id/todo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/todo_off"
                android:onClick="todo_toggle" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center" >
            <ImageView
                android:id="@+id/favourite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fav_off"
                android:onClick="fav_toggle" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center" >
            <ImageView
                android:id="@+id/done"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/done_off"
                android:onClick="done_toggle" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

這是對評論中“ n4h1n”問題的答復。

layout_action_bar.xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="46dp"
    android:orientation="horizontal" >

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5" >

        <ImageView
            android:id="@+id/imageSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/search" />

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="6" >

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="24sp"
            android:textColor="#FF9C00"
            android:text="@string/turipolis" />

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5" >

        <ImageView
            android:id="@+id/imageUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/user" />

    </FrameLayout>

</LinearLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="8dp"
    android:background="@drawable/action_bar_shadow" />

在您的活動XML中:

<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/layout_action_bar" />

我會認真推薦ActionBarSherlock, http: //actionbarsherlock.com/。 這確實是一個很棒的圖書館。 您可以在http://actionbarsherlock.com/download.html上找到其一些代碼演示,並在https://github.com/JakeWharton/ActionBarSherlock/tree/master/actionbarsherlock-samples上找到其源代碼。

但是,我確實記得Google宣布他們已經在其支持庫http://developer.android.com/tools/support-library/index.html中將ActionBar反向移植到2.1。 在您計划使用它而不是擴展普通Activity類的活動中,您將擴展ActionBarActivity類。 相同的邏輯在某種程度上適用於ActionBarSherlock,因為您將擴展SherlockActivity

我要做的就是設置清單中沒有標題欄的主題,並在XML文件中包含一個自定義布局文件即標題欄。 這樣可以確保每個版本都相同。 它的好處是您可以制作所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM