2017-07-10 116 views
0

enter image description here我無法在我的工具欄上顯示菜單圖標。任何想法 ? 下面是menu_main.xml編碼:無法顯示菜單項的圖標

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context=".MainActivity"> 

<item 
    android:id="@+id/menu_date" 
    android:icon="@drawable/clock_logo" 
    android:orderInCategory="100" 
    android:title="Date and Time" 
    app:showAsAction="always"/> 
<item 
    android:id="@+id/menu_location" 
    android:icon="@drawable/location" 
    android:orderInCategory="100" 
    android:title="Location" 
    app:showAsAction="ifRoom"/> 

    </menu> 
+0

你有沒有在你的java文件是什麼?你有沒有關注這篇文章? https://developer.android.com/guide/topics/ui/menus.html#options-menu – Kia

+0

分享你的java代碼 – sumit

+0

檢查這一行是否丟失「<?xml version =」1.0「encoding =」utf-8 「?>」就像這樣:<?xml version =「1.0」encoding =「utf-8」?>

wonderPub

回答

0

在您的主要活動添加

 @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 
    return true; 
    } 
+0

請參考我上面的代碼。 Thx –

0
public class MainActivity extends Activity implements View.OnClickListener { 

private Button analogButton, digitalButton; 
private Toolbar toolbar; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setActionBar(toolbar); 
    getActionBar().setTitle("Clock"); 
    toolbar.setSubtitle("tells the time"); 
    toolbar.setLogo(R.drawable.clock_logo); 
    toolbar.inflateMenu(R.menu.menu_main); 


    analogButton = (Button) findViewById(R.id.button_analog); 
    analogButton.setOnClickListener(this); 
    digitalButton = (Button) findViewById(R.id.button_digital); 
    digitalButton.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_main, menu); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    Fragment fragment; 

    int id = item.getItemId(); 
    switch (id) { 
     case R.id.menu_date: 
      startActivity(new Intent(android.provider.Settings 
        .ACTION_DATE_SETTINGS)); 
      break; 
     case R.id.menu_location: 
      startActivity(new Intent(android.provider.Settings 
        .ACTION_LOCALE_SETTINGS)); 
      break; 
     case R.id.menu_sleep: 
      startActivity(new Intent(android.provider.Settings 
        .ACTION_SOUND_SETTINGS)); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onClick(View v) { 
    Fragment fragment; 
    if (v == analogButton) { 
     fragment = new AnalogFragment(); 
    } else { 
     fragment = new DigitalFragment(); 
    } 
    replaceFragment(fragment); 
} 

public void replaceFragment(Fragment fragment) { 
    FragmentManager manager = getFragmentManager(); 
    FragmentTransaction transaction = manager.beginTransaction(); 
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    transaction.replace(R.id.fragment_container, fragment); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
} 
} 
+0

仍然不起作用。請參考我的java代碼Thx –