如何以编程方式创建导航抽屉?

How to programmatically create a Navigation Drawer?

我需要创建 activity 与导航抽屉只有 java 而没有 xml。但这种方式对我不起作用。是否可以在不使用 xml 的情况下创建并添加到应用程序导航抽屉?我看到的每个示例都有一个额外的 xml 文件和布局。我搜索了很多,但没有得到任何结果。请帮我。这样导航抽屉总是在 contentLayout 上打开,而不是用手指移动或滑动!

我的activity代码:


import androidx.drawerlayout.widget.DrawerLayout;

public class LaunchActivity extends Activity {

    private Context context;
    private DrawerLayout drawerLayout;

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

        //DrawerLayout
        drawerLayout = new DrawerLayout(context);
        drawerLayout.setLayoutParams(new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT));

        //ContentLayout
        LinearLayout contentLayout = new LinearLayout(context);
        contentLayout.setOrientation(LinearLayout.VERTICAL);
        contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        //CustomNavigationView
        RelativeLayout drawerView =  new RelativeLayout(context);
        drawerView.setLayoutParams(new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), RelativeLayout.LayoutParams.MATCH_PARENT));
        drawerView.setGravity(Gravity.START);

        //Add View to ContentLayout
        TextViewFont textView = new TextViewFont(context);
        textView.setText("=");
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(20);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.START);
            }
        });
        contentLayout.addView(textView);

        //Add View to CustomNavigationView
        RelativeLayout drawerHeaderView = new RelativeLayout(context);
        drawerHeaderView.setBackground(getResources().getDrawable(R.drawable.drawer_header));
        drawerHeaderView.setGravity(Gravity.BOTTOM);
        drawerHeaderView.setLayoutParams(new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), AndroidUtilities.dp(170)));
        ImageView imageView = new ImageView(context);
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
        imageView.setBaselineAlignBottom(true);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(AndroidUtilities.dp(90), AndroidUtilities.dp(90));
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        drawerHeaderView.addView(imageView, params);
        drawerView.addView(drawerHeaderView);

        //Add All to DrawerLayout
        drawerLayout.addView(contentLayout, new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT));
        drawerLayout.addView(drawerView, new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), RelativeLayout.LayoutParams.MATCH_PARENT));
        setContentView(drawerLayout);
    }
}

您可以使用这个 library,然后覆盖 BaseActivity 中的 setContentView() 并在那里初始化抽屉生成器。