2017-02-24 121 views
0

我嘗試在工具欄中添加圖標菜單。但我的圖標沒有顯示。在這裏我的代碼。只顯示空白標題。誰能幫我...爲什麼我的菜單圖標不顯示在工具欄中,只顯示空白工具欄

這個片段類

package com.example.bams.task.fragments; 

import android.app.ActionBar; 
import android.app.Fragment; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.FragmentManager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 

import com.example.bams.task.R; 

/** 
* Created by jack on 19/02/2017. 
*/ 

public class AddProjectFragment extends Fragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 

     Toolbar myToolbar = (Toolbar) getActivity().findViewById(R.id.my_toolbar); 
     ((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_add_project, container,false); 
     return rootView; 
    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     //menu.clear(); 
     inflater.inflate(R.menu.header, menu); 
     super.onCreateOptionsMenu(menu, inflater); 
    } 
} 

這是我的自定義工具欄佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/colorBgFragment" 
    android:fitsSystemWindows="true" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/colorBgFragment"> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textPersonName" 
      android:text="Name" 
      android:ems="10" 
      android:id="@+id/editText"/> 
    </LinearLayout> 

</LinearLayout> 

和我的菜單圖標

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/backBtn" 
     android:icon="@drawable/ic_back_filled" 
     app:showAsAction="always" 
     android:title="back"/> 
</menu> 

誰能告訴我什麼我的代碼錯了?

+0

你要爲哪一個主題爲您的應用程序或該片段?還添加截圖。 –

+0

我將