2013-05-27 67 views
3

我想獲得完全像下面的圖片。準備自定義操作欄

enter image description here

但我能得到下面的一個現在。這裏我有兩個問題。

1)如何使這actionbar工作在所有活動,而不是我現在正在膨脹的單一活動。

2)上述圖像好像升高的佈局條,但我看起來像一個正常面板。有人可以幫助我如何實現這些?代碼片斷表示讚賞。

我的電流輸出:

enter image description here

我的佈局代碼膨脹到動作條:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:background="@drawable/ab_customshape" > 

    <ImageButton 
      android:id="@+id/homeicon" 
      android:layout_width="40sp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:src="@drawable/ic_launcher" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" > 
    </ImageButton> 

    <TextView 
     android:id="@+id/title_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="My Title" 
     android:layout_centerVertical="true" 
     android:textColor="#ffffff" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 
    <ImageButton 
     android:id="@+id/rightbutton" 
      android:layout_width="40sp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:src="@drawable/ic_launcher" 
      android:background="@android:color/transparent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
     /> 

</RelativeLayout> 

活動代碼片斷:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     initActionBar(); 
    } 

    private void initActionBar() { 
     mActionBar = getSupportActionBar(); 
     mActionBar.setDisplayShowHomeEnabled(false); 
     mActionBar.setDisplayShowTitleEnabled(false); 
     mInflater = LayoutInflater.from(this); 
     mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
     mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); 
     mTitleTextView.setText("My Own Title"); 
     mActionBar.setCustomView(mCustomView); 
     mActionBar.setDisplayShowCustomEnabled(true); 
    } 

@繪製/ ab_customshape:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <gradient 
     android:angle="-90" 
     android:endColor="#DD2ECCFA" 
     android:startColor="#DD000000" 
     android:type="linear" /> 

</shape> 

回答

1

1)如何使這個動作條要對所有的活動,而不是單一的活動,我現在的工作充氣。

添加下面的代碼到你的style.xml。

<style name="Theme.Styled" parent="Theme.Sherlock.Light"> 
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> 
    <item name="android:actionModeCloseDrawable">@drawable/collapse_button</item> 
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> 
</style> 
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse"> 
    <item name="background">@drawable/ab_customshape</item> 
    <item name="android:background">@drawable/ab_customshape</item> 

</style> 

然後將這個選項設爲您的清單喜歡你的應用程序的主題,

<application 
    android:allowBackup="true" 
    android:icon="@drawable/app_icon" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.Styled" > //Here we set the theme 

並嘗試按鈕添加到您的動作條這樣的,

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 


    menu.add(0, HOMEBUTTON, 0, "Home").setIcon(R.drawable.home_button) 

    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 

    return true; 
} 

enter image description here

和9補丁這裏enter image description here

+0

你是什麼意思的提升佈局欄?請解釋一下,只有我可以幫忙。 –

+0

感謝您的回覆。但根據您的代碼,操作項目和主頁圖標根據操作欄樣式顯示,我的意思是隻有有空房間。如果我錯了,請糾正我。關於第二個問題,你可以看到,在動作條的佈局似乎它比動作條下面的畫面時看起來升高。在我的帖子的第一張圖片中清楚地看到。 – Spike

+0

您可以設置按鈕,以顯示總是喜歡,.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); –