【问题标题】:How to inflate two different menus for a Toolbar and a BottomAppBar如何为 Toolbar 和 BottomAppBar 充气两个不同的菜单
【发布时间】:2020-06-23 20:55:26
【问题描述】:

以下情况会导致模拟器中的安装后崩溃。没有显示调试信息。

package com.example.myapplication;

import android.os.Bundle;

import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.tabs.TabLayout;

import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.myapplication.ui.main.SectionsPagerAdapter;
public class MainActivity extends AppCompatActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.top_bar, menu);
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar top_toolbar = findViewById(R.id.top_toolbar);
        setSupportActionBar(top_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        BottomAppBar bottom_toolbar = findViewById(R.id.bottom_toolbar);
        bottom_toolbar.replaceMenu(R.menu.bottom_bar);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        ImageButton my_posts_button = findViewById(R.id.my_posts_button);
        ImageButton chats_button = findViewById(R.id.chats_button);
        ImageButton post_button = findViewById(R.id.post_button);
        ImageButton settings_button = findViewById(R.id.settings_button);
        ImageButton profile_button = findViewById(R.id.profile_button);
        ImageButton sort_button = findViewById(R.id.search_button);
        ImageButton search_button = findViewById(R.id.sort_button);
    }
}

看起来网站认为我的帖子主要是代码。看起来网站认为我的帖子主要是代码。看起来网站认为我的帖子主要是代码。

【问题讨论】:

  • 2 个不同的操作栏?你能解释一下你想要达到的目标吗?
  • 我有一个用于设置等的顶部栏和一个用于配置文件、聊天等(所有图标)的底部栏。我对 android studio 很陌生,所以我当然可能在选择方法时出错,而且我知道 python(自学)并且正在尝试学习用 java 制作应用程序。

标签: android android-studio android-toolbar android-bottomappbar


【解决方案1】:

您同时使用ToolbarBottomBar。您只能将onCreateOptionsMenu 用于setSupportActionBar 中使用的栏。

例如:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

onCreateOptionsMenu 用于 Toolbar 菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return true;
}

那么对于 BottomBar,您可以在布局中使用 app:menureplaceMenu 方法:

    BottomAppBar bottomAppBar = findViewById(R.id.bottomappbar);
    bottomAppBar.replaceMenu(R.menu.bottom_bar);
    bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item == R.id...){
                //....
                return true;
            }
            return false;
        }
    });

【讨论】:

  • @noe.ramelo 检查stacktrace是否与代码相关
  • 改成:android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar后得到:pastebin.pl/view/547d8c77
  • 已通过将 ImageButton 更改为 View 来修复。奇怪的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多