简体   繁体   English

向(打开/关闭)导航抽屉的活动添加(打开/关闭)按钮

[英]Add (open/close) button to activity for (opening/closing) the navigation drawer

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 我目前正在从android站点了解导航抽屉,我正在使用他们的示例http://developer.android.com/training/implementing-navigation/nav-drawer.html

What I want is to add a button in the MainActivity which would be able to open the NavigationDrawer . 我想要的是在MainActivity添加一个按钮,该按钮可以打开NavigationDrawer I need to do it programmatically, not in XML. 我需要以编程方式,而不是XML。 How can I do that? 我怎样才能做到这一点?

Create a method in MainActivity which contains your drawerLayout. MainActivity创建一个包含drawerLayout的方法。

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


and from Your fragment In oncreateView() method As you want new Button Programmatically add Button in Your Root inflated layout. 来自你的片段在oncreateView()方法中你想要新的Button按编程方式在你的Root膨胀布局中添加Button。 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;
    }
    }
 }


You can try this code in your fragment.. 您可以在片段中尝试此代码..

Create your Button in onCreate(Bundle) method: onCreate(Bundle)方法中创建Button:

Button button = new Button(this);

Find your DrawerLayout : 找到你的DrawerLayout

mDrawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout_id);

Set an OnClickListener on this button: 在此按钮上设置OnClickListener

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        mDrawerLayout.openDrawer(Gravity.LEFT);   
    }
)

This will give you an empty drawer. 这会给你一个空的抽屉。 If you have a View that you would like to place inside the drawer, replace: 如果您有想要放置在抽屉内的View ,请替换:

mDrawerLayout.openDrawer(Gravity.LEFT);

with: 有:

mDrawerLayout.openDrawer(myCustomView);

If you want the button to toggle the drawer(close the drawer if its open or, open it if its closed) use the following OnClickListener : 如果您希望按钮切换抽屉(如果抽屉打开则关闭抽屉,或者如果抽屉关闭则打开它)使用以下OnClickListener

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawer(Gravity.LEFT);
        } else {
            mDrawerLayout.openDrawer(Gravity.LEFT); 
        }  
    }
)

If you are using a custom view, use this OnClickListener : 如果您使用的是自定义视图,请使用此OnClickListener

button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        if (mDrawerLayout.isDrawerOpen(myCustomView)) {
            mDrawerLayout.closeDrawer(myCustomView);
        } else {
            mDrawerLayout.openDrawer(myCustomView); 
        }  
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM