本文介绍了添加(开/关)按钮活动(开/关)的抽屉式导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前了解的导航抽屉从Android网站,我使用他们的榜样http://developer.android.com/training/implementing-navigation/nav-drawer.html

I am currently learning about the Navigation Drawer from the android site, and I am using their example http://developer.android.com/training/implementing-navigation/nav-drawer.html

我要的是在添加一个按钮 MainActivity 这将能够打开 NavigationDrawer 。我需要做编程,而不是在XML。我该怎么办呢?

What I want is to add a button in the MainActivity which would be able to open the NavigationDrawer. I need to do it programmatically, not in XML. How can I do that?

推荐答案

创建了一个方法 MainActivity ,其中包含您drawerLayout。

Created a method in MainActivity which contains your drawerLayout.

public void open()
{
    mDrawerLayout.openDrawer(Gravity.LEFT);
}


从您的片段在oncreateView()方法,只要你想新的编程键在根膨大布局添加按钮。您的片段有按钮
波纹管我修改片段尝试


and from Your fragmentIn oncreateView() method As you want new Button Programmatically add Button in Your Root inflated layout. Your fragment has button
bellow I modified fragment try

 public static class PlanetFragment extends Fragment {
    public static final String ARG_PLANET_NUMBER = "planet_number";

    public PlanetFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
        int i = getArguments().getInt(ARG_PLANET_NUMBER);
        String planet = getResources().getStringArray(R.array.planets_array)[i];

        int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                        "drawable", getActivity().getPackageName());
        ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
        getActivity().setTitle(planet);
        RelativeLayout root=(RelativeLayout)rootView.findViewById(R.id.root);
        Button button=new Button(getActivity());
        LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        button.setText("openDrawer");
        root.addView(button);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ((MainActivity)getActivity()).open();
            }
        });
        return rootView;
    }
    }
 }


您可以在片段试试这个code ..


You can try this code in your fragment..

这篇关于添加(开/关)按钮活动(开/关)的抽屉式导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:54