以编程方式添加NavigationDrawer

时间:2016-11-22 14:48:34

标签: android navigation-drawer drawerlayout programmatically

我正在尝试以编程方式将NavigationDrawer添加到我的活动中,但是当它被添加时,我无法与屏幕的其余部分进行交互,例如按钮不是可点击的。添加de NavigationDrawer的方法是:

//The activity view
final ViewGroup viewActvity = (ViewGroup) this.findViewById(android.R.id.content);

//The custom view to show in the NavigationDrawer
LinearLayout menu = new LinearLayout(this);

//The DrawerLayout
DrawerLayout drawerLayout = new android.support.v4.widget.DrawerLayout(this);

float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 280, getResources().getDisplayMetrics());
DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams((int) width, LinearLayout.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.START;

LinearLayout drawerPanel = new LinearLayout(this);
drawerPanel.setGravity(Gravity.START);
drawerPanel.setClickable(true);
drawerPanel.setLayoutParams(lp);
drawerPanel.addView(menu, lp);
drawerPanel.setOrientation(LinearLayout.VERTICAL);

drawerLayout.addView(drawerPanel, lp);

viewActvity.addView(drawerLayout);

有什么想法吗?谢谢

0 个答案:

没有答案