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

时间:2019-06-22 06:59:55

标签: java android drawerlayout navigation-drawer drawer

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

我的活动代码:


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

1 个答案:

答案 0 :(得分:0)

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