2014-10-10 54 views
5

我正在製作一個Android應用程序,它有一個操作欄,操作欄包含兩側的菜單項。所以,我需要在左側添加一個菜單項,並在操作欄的右側添加一個菜單項。如何將菜單項設置到操作欄的左側?

爲此,我創建了自定義佈局並將其設置爲操作欄。如圖所示,下面的圖片

action bar having custom layout

中,但我只是想有在左側的地方菜單項和右邊的其他截圖顯示菜單項的操作欄

actionbar with menu itemss

在此操作欄中包含菜單項。所以,我只是想把加號按鈕菜單項左側作爲第一個屏幕截圖相同,但我不想爲此使用自定義佈局,

此外,我需要chnage操作欄標題的字體樣式和把它放在中心位置。

請幫幫我。我在這上面花了很多時間。

謝謝

回答

1

您必須創建一個自定義視圖。你不能在你的用例中使用選項菜單通貨膨脹。

將活動的風格設置爲Theme.AppCompat,並且不要在活動或應用程序樣式中使用NoActionBar。

創建一個custom_menu佈局。

<?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="fill_parent"> 
    <ImageButton android:src="@drawable/add" 
       android:background="?attr/actionBarItemBackground" 
       android:layout_width="?attr/actionBarSize" 
       android:layout_height="?attr/actionBarSize" 
       android:scaleType="centerInside" 
       android:id="@+id/action_add" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true"/> 

    <TextView android:text="NewsFeed" 
       android:background="?attr/actionBarItemBackground" 
       android:layout_height="?attr/actionBarSize" 
       android:layout_width="wrap_content" 
       android:scaleType="centerInside" 
       android:id="@+id/text_appname" 
       android:textSize="30sp" 
       android:textStyle="bold" 
       android:layout_marginLeft="5dp" 
       android:textColor="#FFF" 
       android:paddingTop="5dp" 
       android:layout_toRightOf="@+id/action_add" 
       android:layout_alignParentTop="true"/> 

    <ImageButton android:src="@drawable/search" 
       android:background="?attr/actionBarItemBackground" 
       android:layout_width="?attr/actionBarSize" 
       android:layout_height="?attr/actionBarSize" 
       android:scaleType="centerInside" 
       android:id="@+id/action_search" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true"/> 
</RelativeLayout> 

在您的活動的OnCreate添加下面的代碼

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_mainfeed); 
    ActionBar mActionBar = getSupportActionBar(); 
    mActionBar.setDisplayShowHomeEnabled(false); 
    mActionBar.setDisplayShowTitleEnabled(false); 
    LayoutInflater li = LayoutInflater.from(this); 
    View customView = li.inflate(R.layout.custom_menu, null); 
    mActionBar.setCustomView(customView); 
    mActionBar.setDisplayShowCustomEnabled(true); 
    ImageButton addContent = (ImageButton) customView.findViewById(R.id.action_add); 
    addContent.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // ... 
     } 
    }); 

    ImageButton search = (ImageButton) customView.findViewById(R.id.action_search); 
    search.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // ... 
     } 
    }); 
} 
0

您可以導航繪製直接添加到工具欄試試這個代碼:

yourToolbar.setNavigationIcon("Resource id") 

yourToolbar.setNavigationOnClickListener(new ClickListener(){}) 
相關問題