2013-08-21 151 views
0

我正在嘗試創建自定義標題,但我不知道如何操作。我一直在研究,但我總是有風格和東西,我的日食不會有問題。 事情是,我想要的東西很簡單。Android的自定義操作欄/標題

中的Android 4.2.2的屏幕會是這樣的:

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

我想,這就是Android SDK 11等的默認標題。我一直在測試它更改清單android:targetSdkVersion和我的手機應用程序的外觀變化。

問題是不同Android版本之間的兼容性。我見過的應用程序在不同的設備上工作,這是結果:

在安卓4.0.4

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

在安卓2.3.6

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

你可以注意標題,甚至顏色和形狀如何變化。 如果我創建自定義標題,它會在每個設備中以相同的方式看到,還是取決於Android版本?

我想爲每個設備製作應用程序的唯一版本,這可能嗎? 如果可能的話,我想要第一張圖片的下一個標題,並且在紅色的矩形內部,我想插入一張圖片。如果我可以保留選項菜單(黃色方塊),那麼更好。如果由於Android版本而無法使用,使用硬件按鈕無法激活。 在此先感謝!

+0

在這種情況下,我會去爲一個XML文件中的線性佈局實現了自定義操作欄,並與標籤,包括它。 –

+0

@DídacPérez,我該怎麼做? – n4h1n

回答

1

這是不實際的,可能不推薦,但我做的是我用完全隱藏的動作條/頭的Android清單:

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

然後,我的活動佈局的xml文件中,我簡單地定製構建一個看起來像操作欄/標題的標題。這裏是我的應用程序的一個例子:

enter image description here

即在XML文件中完全做到,但它看起來真實的,因爲正確的動作條/頭。

編輯

這是我的導航欄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> 
+0

這是一個好主意,你能給我一個代碼的例子嗎? – n4h1n

+0

太棒了!這非常有用! – n4h1n

+0

不客氣。 – 2013-08-21 09:13:52

0

我會做的只是設置一個主題,在您的清單中沒有標題欄,並在您的XML文件,該文件是你的標題欄自定義佈局文件。這確保它在每個版本中都是一樣的。它的好處是你可以做到一切。

+0

我該怎麼做?我見過類似的東西,我試過了,但標題欄不會出現。如果我用tittle bar設置了這個佈局,選項菜單將消失,我不能將它包含在Android版本中選項按鈕在欄中。你知道任何一個頁面,我可以找到你正在談論的這個教程嗎? – n4h1n

+0

請參閱下面的答案...它顯示瞭如何隱藏清單中的TitleBar。然後在活動的Layout xml文件中,構建自定義標題欄。 – 2013-08-21 08:17:50

1

這是評論的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" /> 
+0

我在哪部分活動中應該包含?我得到的標題圖像,但沒有在正確的位置 – n4h1n

+0

在我的情況下,我的整個活動是垂直水平佈局,所以我把包含作爲佈局的第一個元素。 –

1

我會認真建議ActionBarSherlock,http://actionbarsherlock.com/。它確實是一個很棒的圖書館。你可以找到一些它的代碼演示在這裏http://actionbarsherlock.com/download.html,這裏https://github.com/JakeWharton/ActionBarSherlock/tree/master/actionbarsherlock-samples

他們的源代碼,但是我記得谷歌宣佈,他們已經回遷的動作條,以2.1的支持庫這裏http://developer.android.com/tools/support-library/index.html。在您打算使用它而不是擴展正常Activity類的活動中,您將擴展ActionBarActivity類。同樣的邏輯也有些適用於ActionBarSherlock,因爲你會被延長SherlockActivity

+0

沒錯,許多開發人員都使用該ActionBarSherlock,但正如Adam所說:「添加了ActionBar以允許將操作欄用戶界面設計模式實施回Android 2.1(API級別7)及更高版本。使用這個類需要你通過擴展新的ActionBarActivity類來實現你的活動。' –