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 :(得分:0)
在您的主要活动中添加
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
答案 1 :(得分: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();
}
}