应用程序名称或菜单在工具栏

时间:2017-08-14 19:45:50

标签: android xml menu android-actionbar toolbar

我在活动中添加了一个工具栏并在活动中添加了一个菜单,但菜单消失了,甚至是工具栏中的应用程序名称,工具栏也是空白。

我补充说:

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

但申请被强制关闭。

到目前为止我的代码:

主要活动(我在此活动中使用工具栏)

  private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list = (ListView)findViewById(R.id.list1);
   toolbar=(Toolbar) findViewById(R.id.app_bar_layout);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    NavigationDrawerFragent drawerlayout = (NavigationDrawerFragent) 
 getSupportFragmentManager().findFragmentById
 (R.id.fragment_navigation_drawer);

drawerlayout.setUp((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,R.layout.list_item,R.id.txt,item);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
 position, long id) {
            if (position==0){
                Intent intent = new Intent(List.this,biographi.class);
                startActivity(intent);
            }else if(position==1){
               Intent intent1 = new Intent(List.this,Rooidad.class);
                startActivity(intent1);
            }else if(position==2){
                Intent intent1 = new Intent(List.this,Asar.class);
                startActivity(intent1);
            }
        }
    });
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.drawer, menu);

    return super.onCreateOptionsMenu(menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id==R.id.setting)
    {
        Toast.makeText
 (getApplicationContext(),"setting",Toast.LENGTH_LONG).show();
        return true;
    }
    else if(id==R.id.forward)
    {
        Toast.makeText]
 (getApplicationContext(),"forward",Toast.LENGTH_LONG).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
    }

app_bar xml (工具栏布局)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/primaryColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ThemeOverlay.AppCompat.Light"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>

main xml (主要活动的布局)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bax"
    tools:context="tmbax.ir.List">
    <include layout="@layout/app_bar"
        android:id="@+id/app_bar"/>
    <ListView
        android:id="@+id/list1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:dividerHeight="10dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/app_bar" />
</RelativeLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="tmbax.ir.NavigationDrawerFragent"
    app:layout="@layout/fragment_drawer_layout"
    tools:layout="@layout/fragment_drawer_layout" />
 </android.support.v4.widget.DrawerLayout>

xml菜单

<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="tmbax.ir.List">
    <item
        android:id="@+id/setting"
        android:title="setting"
        android:orderInCategory="100"
        app:showAsAction="never"/>
    <item
        android:id="@+id/forward"
        android:title="forward"
        android:icon="@mipmap/ic_forward"
        android:orderInCategory="200"
        app:showAsAction="ifRoom" />

</menu>

如果我不能很好地写英文,我很抱歉,请帮助我。

0 个答案:

没有答案