为了账号安全,请及时绑定邮箱和手机立即绑定

如何将菜单设置为 AppBarLayout

如何将菜单设置为 AppBarLayout

撒科打诨 2023-08-16 17:42:44
我是编程新手,我想添加一个设置菜单按钮。如图所示,我的应用程序不显示三点按钮。MainActivity.java :public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        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);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.settings, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemId()) {            case R.id.vueSatellite:                Toast.makeText(this, "Hi", Toast.LENGTH_SHORT).show();                return true;            default:                return super.onOptionsItemSelected(item);        }    }}Activity_main.xml:<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayout     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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <androidx.viewpager.widget.ViewPager        android:id="@+id/view_pager"        android:layout_width="match_parent"        android:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_view_behavior" />    <com.google.android.material.appbar.AppBarLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/AppTheme">
查看完整描述

1 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

在您的活动中只需添加

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

希望它有效。


查看完整回答
反对 回复 2023-08-16
  • 1 回答
  • 0 关注
  • 59 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信