Android操作栏按钮不会显示带有fragment活动的导航抽屉

时间:2014-12-25 12:23:48

标签: java android android-fragments android-actionbar

我想在动作栏中显示一些按钮,但它们只是不显示 我已经使用片段成功实现了导航抽屉和viewpager 显示菜单充气器和操作栏按钮的实现也已完成,但它们不会显示

菜单/ main.xml中的

我添加了app:showAsAction =" always"但它仍然不会出现

这是

detailactivity.java

package com.test.app;

import java.lang.reflect.Field;

import android.app.ActionBar;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
//import android.support.v7.app.ActionBar;
import com.Blog.gkgyan.parser.RSSFeed;


//public class DetailActivity extends FragmentActivity implements OnItemClickListener{
public class DetailActivity extends FragmentActivity implements OnItemClickListener{
    RSSFeed feed;
    int pos;
    private DescAdapter adapter;
    private ViewPager pager;

    private DrawerLayout drawerLayout;
    private ListView listView;
    private  String[] planets;

    private ActionBarDrawerToggle drawerListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.detail);

        //Drawer Layout
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerListener = new ActionBarDrawerToggle(this, drawerLayout,R.drawable.ic_drawer,R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerClosed(View drawerView) {
                // TODO Auto-generated method stub
                //super.onDrawerClosed(drawerView);
                Toast.makeText(DetailActivity.this, "Drawer closed", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                //super.onDrawerOpened(drawerView);
                Toast.makeText(DetailActivity.this, "Drawer opened", Toast.LENGTH_LONG).show();
            }
        };

        drawerLayout.setDrawerListener(drawerListener);
        //getSupportActionBar().setHomeButtonEnabled(true);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);;

        getActionBar().setDisplayHomeAsUpEnabled(true);

//      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();
//      }

        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {        
            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(); }

        }


        listView = (ListView) findViewById(R.id.drawerlist);
        planets = getResources().getStringArray(R.array.planets);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, planets ));
        listView.setOnItemClickListener(this);

        // Get the feed object and the position from the Intent
        feed = (RSSFeed) getIntent().getExtras().get("feed");
        pos = getIntent().getExtras().getInt("pos");

        // Initialize the views
        adapter = new DescAdapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.pager);

        // Set Adapter to pager:
        pager.setAdapter(adapter);
        pager.setCurrentItem(pos);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        drawerListener.onConfigurationChanged(newConfig);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
        //return super.onCreateOptionsMenu(menu);
    }

    //@Override
    //public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        if (drawerListener.onOptionsItemSelected(item)) {
            return true;
        }

        Intent i = null;
        switch (item.getItemId()) {
        case R.id.action_rate:
            String webpage = "http://developer.android.com/index.html";

            i = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
            startActivity(i);
            //return true;
            break;

        case R.id.action_share:
            i = new Intent();
            i.setAction(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_TEXT, "Hello from Hansel and Petal!");
            i.setType("text/plain");
            startActivity(i);
            //return true;
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerListener.syncState();
    }

    public class DescAdapter extends FragmentStatePagerAdapter {
        public DescAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return feed.getItemCount();
        }

        @Override
        public Fragment getItem(int position) {

            DetailFragment frag = new DetailFragment();

            Bundle bundle = new Bundle();
            bundle.putSerializable("feed", feed);
            bundle.putInt("pos", position);
            frag.setArguments(bundle);

            return frag;
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        if (position == 0) {
            Intent intent = new Intent(this, GkcategoryListActivity.class);
            startActivity(intent);
        }
        else if (position == 1) {
            Intent intent = new Intent(this, GkcategoryListActivity.class);
            startActivity(intent);
        }
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
    }

}

`

和menu / main.xml

<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="com.Blog.gkgyan.MainActivity" >

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

    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_action_share"
        android:orderInCategory="101"
        android:title="@string/action_share"
        app:showAsAction="always" />

    <item
        android:id="@+id/action_rate"
        android:icon="@drawable/ic_action_important"
        android:orderInCategory="101"
        android:title="@string/action_rate"
        app:showAsAction="always"/>

</menu>

我要在 detailactivity.java 的操作栏中显示action_share和Action_rate按钮,尝试在此页面上无效的任何内容

然而它在main activity.java中完全正常工作

这是代码

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

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


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        Intent intent = null;
        switch (item.getItemId()) {
        case R.id.action_rate:
            String webpage = "http://developer.android.com/index.html";

            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
            startActivity(intent);
            break;

        case R.id.action_share:
            intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, "Hello from Hansel and Petal!");
            intent.setType("text/plain");
            startActivity(intent);
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }


    public void latestActivity(View v){
        //Intent intent = new Intent(this, SplashActivity.class);
        Intent intent = new Intent(this, GkcategoryListActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

    }


}

这是片段活动或导航抽屉的问题任何帮助都会很棒请提及我的代码应该在哪里进行更改

0 个答案:

没有答案