2016-09-19 103 views
0

我想在右侧的工具栏上设置“保存”,但它列在列表中。我有两个菜单文件第一个我用于绘制菜单,第二个是SAVE按钮。设置工具栏上的按钮

The save button is appearing but its not on the toolbar how can i get this on toolbar

<?xml version="1.0" encoding="utf-8"?> 
 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
 

 

 
    <group android:checkableBehavior="single"> 
 
     <item 
 
      android:id="@+id/home" 
 
      android:title="@string/home"></item> 
 

 
     <item 
 
      android:id="@+id/quote" 
 
      android:title="@string/quote"></item> 
 
     <item 
 
      android:id="@+id/inbox" 
 
      android:title="@string/inbox"></item> 
 
     <item 
 
      android:id="@+id/services" 
 
      android:title="@string/services"></item> 
 
    </group> 
 

 
    <group android:checkableBehavior="single"> 
 
     <item 
 
      android:id="@+id/setting" 
 
      android:title="@string/setting"></item> 
 
    </group> 
 

 
    <group android:checkableBehavior="single"> 
 
     <item 
 
      android:id="@+id/contact" 
 
      android:title="@string/contact"></item> 
 
     <!-- <item 
 
      android:id="@+id/logout" 
 
      android:title="@string/logout"></item>--> 
 
    </group> 
 

 
</menu>

这是第二个菜单用于保存按钮

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

此主我曾经第一个菜单文件绘制导航视图活动

@Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu1111; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.drawe_menu, menu); 
 
     return false; 
 
    } 
 

 
    @Override 
 
    public boolean onPrepareOptionsMenu(Menu menu) { 
 
     /* MenuItem item= menu.findItem(R.menu.drawe_menu); 
 
     item.setVisible(true);*/ 
 
     super.onPrepareOptionsMenu(menu); 
 
     return true; 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     if (actionBarDrawerToggle.onOptionsItemSelected(item)) { 
 
      return true; 
 
     } 
 
     return super.onOptionsItemSelected(item); 
 
    }

我呼吁一号菜单

有方法的,这才是我想要保存工具栏上的按钮fragmnet

@Override 
 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflator) { 
 
     inflator.inflate(R.menu.menu, menu); 
 
     super.onCreateOptionsMenu(menu, inflator); 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     Log.d("", "onOptionsItemSelected"); 
 
     return super.onOptionsItemSelected(item); 
 
    } 
 

 
    @Override 
 
    public void onPrepareOptionsMenu (Menu menu) { 
 
     menu.findItem(R.id.home).setVisible(false); 
 
     menu.findItem(R.id.setting).setVisible(false); 
 
     menu.findItem(R.id.contact).setVisible(false); 
 
     menu.findItem(R.id.quote).setVisible(false); 
 
     menu.findItem(R.id.inbox).setVisible(false); 
 
     menu.findItem(R.id.services).setVisible(false); 
 
     menu.findItem(R.id.save).setVisible(true); 
 
     super.onPrepareOptionsMenu(menu); 
 
    }

回答

0

您可以通过创建自定义工具栏来设置按钮。 检查下面的链接以在工具栏上设置按钮。 Creating a button in Android Toolbar

,如果你想添加按钮,工具栏,这将是可见的总是 那么你的工具栏中添加一个按钮,重力右

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize"> 
<Button 
android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="right" 
/> 
</android.support.v7.widget.Toolbar> 
+0

则该按钮将在所有布局 –

+0

可见

+0

它在哪里以及如何工作,如果我在我的片段中添加这个 –

0
1.Create the full menu, including the item that only should be displayed 
for a particular page, in the main activity the usual way. Save a reference 
to the menuItem that changes for each page in the Application object. 

2.Implement the onPageChangelistener as above, and set the visibility of 
the menuItem as needed for each page. You get access to the menuItem from the 
Application object. This keeps the menu items as you want them in a 
particular phone orientation, but will fail when the orientation of the  phone changes. Save the current page position in the Application  object. Be sure to initialize the page position in the Application object to the first page (ie 0). 

3.In the onCreateOptionsMenu in the main activity, use the current page 
position (found in the Application object) to determine if the menuItem 
should be visible or not. This will take care of screen rotations. 
+0

我可以实现这个使用片段事务而不是页面更改 –