Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

android - icon disappeared in action bar after enabling a new toolbar

I want to design a split action toolbar (I know that it was removed since android 5.0 lollipop).

The below example showed me a new toolbar, but the icons on the action bar have now disappeared (because I inflate another menu). How to display the icons on the action bar and have toolbar at the same time? How to inflate 2 menus, one for the action bar and the other for toolbar?

Example

@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));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you can refer to the following sample (the activity_main.xml looks like the one I posted in your previous question, but now edited - adding buttons inside top toolbar)

<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>

Then in your activity:

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:
    }
}
}

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...