使用getMenuInflater(),无法显示菜单Menu选项

解决方法:

将使用菜单栏的Activity(PullFreshActivity为当前活动的名称)中的

public class PullFreshActivity extends Activity

改为

public class PullFreshActivity extends AppCompatActivity

运行结果:

使用getMenuInflater(),无法显示菜单Menu选项_第1张图片

MY_APP为项目名称, MY_APP\app\src\main\res\menu\pull_type.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".MainActivity" >
    <item android:id="@+id/action_material"
        android:title="Material"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/action_circles"
        android:title="Circles"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/action_water_drop"
        android:title="WaterDrop"
        android:orderInCategory="100"
        app:showAsAction="never"/>

    <item android:id="@+id/action_ring"
        android:title="Ring"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/action_smartisan"
        android:title="Smartisan"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

使用菜单栏的页面 PullFreshActivity关于menu代码如下:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.pull_type, menu);//R.menu.pull_type为菜单样式,为前一个代码
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) { //对菜单的不同选项进行内容设置
        int id = item.getItemId();
        //5种不同的样式
        switch (id){
            case R.id.action_material:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL); //改变主题样式
                return true;
            case R.id.action_circles:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES); 
                return true;
            case R.id.action_water_drop:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
                return true;
            case R.id.action_ring:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_RING);
                return true;
            case R.id.action_smartisan:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_SMARTISAN);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

使用菜单栏的页面 PullFreshActivity完整代码:

package com.example.my_app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.baoyz.widget.PullRefreshLayout;
import java.lang.reflect.Field;

public class PullFreshActivity   extends AppCompatActivity {

    private  PullRefreshLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_fresh); //当前页面使用的布局文件

        String[] array = new String[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = "test " + i;
        }

        final ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(new android.widget.ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));

        layout = (PullRefreshLayout) findViewById(R.id.prl_view);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        showDialog();
                    }
                }, 3000);
            }
        });

    }

    private void showDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
//        builder.setIcon(R.drawable.picture);
        builder.setTitle("提示信息");
        builder.setMessage("刷新成功!");
        builder.setPositiveButton("我知道了",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
        AlertDialog dialog=builder.create();
        dialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.pull_type, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //5种不同的样式
        switch (id){
            case R.id.action_material:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);
                return true;
            case R.id.action_circles:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES);
                return true;
            case R.id.action_water_drop:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
                return true;
            case R.id.action_ring:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_RING);
                return true;
            case R.id.action_smartisan:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_SMARTISAN);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private void getOverflowMenu() {
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if(menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Android)