第二个工具栏未出现

时间:2018-06-26 11:13:50

标签: java android toolbar

我在向活动中添加第二个工具栏时遇到问题。首先,它们的行为类似于操作栏,其次应该出现在屏幕中间。我已经尝试过像这里Set menus for multiple toolbars on android中建议的人们那样做,没有任何运气。第二个工具栏(我在onCreate()中为菜单添加一个菜单)根本没有出现。有什么建议吗?

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar myToolbar = findViewById(R.id.my_toolbar);
    Toolbar quickMenu = findViewById(R.id.quickMenu);
    setSupportActionBar(myToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    quickMenu.inflateMenu(R.menu.quickmenu);
    quickMenu.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem arg0) {
            if(arg0.getItemId() == R.id.action_shuffle){

            }
            return false;
        }
    });

我的onCreateOptionMenu()

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.actionbuttons, menu);
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);
    return true;
}

ActivityMain

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintBottom_toTopOf="@+id/mainMenu"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.Toolbar>

<ListView
    android:id="@+id/mainMenu"
    android:layout_width="400dp"
    android:layout_height="150dp"
    android:layout_marginEnd="6dp"
    android:layout_marginStart="3dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@id/songsList"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias=".0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.Toolbar
    android:id="@+id/quickMenu"
    android:layout_width="400dp"
    android:layout_height="50dp"
    android:layout_marginEnd="6dp"
    android:layout_marginStart="3dp"
    app:layout_constraintBottom_toTopOf="@id/songsList"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/mainMenu"
    app:layout_constraintVertical_bias="0.0" />

<ListView
    android:id="@+id/songsList"
    android:layout_width="400dp"
    android:layout_height="0dp"
    android:layout_marginEnd="6dp"
    android:layout_marginStart="3dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/quickMenu"
    app:layout_constraintVertical_bias="0" /> 
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案