启用新工具栏后,操作栏中的图标消失



我想设计一个拆分操作工具栏(我知道它被删除,因为android 5.0棒棒糖)。

下面的例子给我展示了一个新的工具栏,但是操作栏上的图标现在消失了(因为我膨胀了另一个菜单)。如何同时显示操作栏上的图标和工具栏?如何膨胀两个菜单,一个用于操作栏,另一个用于工具栏?

例子
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  Toolbar tb=(Toolbar)findViewById(R.id.toolbar);
  tb.inflateMenu(R.menu.actions);
  tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener({
    @Override
    public boolean onMenuItemClick(MenuItem item) {
      return(onOptionsItemSelected(item));
    }
  });
  return(super.onCreateOptionsMenu(menu));
}

我认为您可以参考以下示例(activity_main.xml看起来像我在您之前的问题中发布的那个,但现在编辑-在顶部工具栏内添加按钮)

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/button1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_action_cloud" />
            <ImageButton
                android:id="@+id/button2a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_action_copy" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_bottom"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/button1"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.175"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_action_back" />
            <View
                android:id="@+id/view1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0.1" />
            <ImageButton
                android:id="@+id/button2"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.175"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_action_cloud" />
            <View
                android:id="@+id/view2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0.1"
                android:background="@android:color/transparent" />
            <ImageButton
                android:id="@+id/button3"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.175"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_action_copy" />
            <View
                android:id="@+id/view3"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0.1"
                android:background="@android:color/transparent" />
            <ImageButton
                android:id="@+id/button4"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.175"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_action_help" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
</RelativeLayout>

然后在你的活动中:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ImageButton button1 = (ImageButton) findViewById(R.id.button1);
    button1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Toast.makeText(mContext, "action_settings", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button1:
            Toast.makeText(mContext, "button1", Toast.LENGTH_SHORT).show();
            break;
        default:
    }
}
}

希望这对你有帮助!

最新更新