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

时间:2015-10-23 07:24:07

标签: android

我想设计一个拆分动作工具栏(我知道它已经从Android 5.0棒棒糖中删除了。)

以下示例向我展示了一个新的工具栏,但操作栏上的图标现已消失(因为我给另一个菜单充气)。如何在操作栏上显示图标并同时拥有工具栏?如何为2个菜单充气,一个用于操作栏,另一个用于工具栏?

示例

@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));
}

1 个答案:

答案 0 :(得分:1)

我认为您可以参考以下示例(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:
    }
}
}

希望这有帮助!

相关问题