dynames0098
2/21/2018 - 2:09 AM

Actionbar添加菜单

menu先要添加资源文件/res/menu右键new一个menu文件

<?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">
    <item
        android:id="@+id/action_download"
        android:icon="@drawable/ic_file_download_white_24px"
        android:orderInCategory="1"
        android:title="download"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_share_white_24px"
        android:orderInCategory="2"
        android:title="share"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_view"
        android:icon="@drawable/ic_open_in_browser_white_24px"
        android:orderInCategory="3"
        android:title="open in browser"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_about"
        android:icon="@drawable/ic_build_white_24px"
        android:orderInCategory="4"
        android:title="About"
        app:showAsAction="never"/>
</menu>

在Activity中添加

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id==R.id.action_download){
        if(checkWritePermission()){
            startDownloadFile();
        }else {
            requestWritePermission();
        }
    }
    if(id==R.id.action_share){
        Intent intent=new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT,"share");
        intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.read_paper)+article.getPdf());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, "share"));
    }
    if(id==android.R.id.home){
        onBackPressed();
    }
    if(id==R.id.action_view){

        String url = article.getPdf().replace("pdf","abs").substring(0,article.getPdf().length()-4);
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
    if(id==R.id.action_about){
        invoke();
    }
    return super.onOptionsItemSelected(item);
}