将导航抽屉滑动为相对布局,以编程方式完成

时间:2016-06-15 20:52:04

标签: android layout relativelayout drawer

我的导航抽屉有问题。

我设置了抽屉布局和一切,但是当它涉及到真正的抽屉窗格时#34;我想打开它,我得到一个例外说:

  

java.lang.IllegalArgumentException:查看   android.widget.RelativeLayout {699e897 V.E ...... ........   0,0-1440,2464}不是滑动抽屉

所以这是代码:

drawerPane=new RelativeLayout(activity);   
RelativeLayout.LayoutParams drawerPane_params=new
RelativeLayout.LayoutParams(280, ViewGroup.LayoutParams.MATCH_PARENT);
drawerPane.setGravity(Gravity.START);

drawerPane.setLayoutParams(drawerPane_params);

drawerLayout=(DrawerLayout)activity.getRootView();
//getRootView is a custom function, returns the contentview (see updates below)
drawerLayout.addView(drawerPane);

在这段代码中,drawerlayout是我活动的RootView(名为:" activity")

我有一个按钮,可以在按钮上打开我的抽屉,代码是这样的:

drawerLayout.openDrawer(drawerPane);

此处显示错误。

有什么建议吗?感谢

更新

这是我用来将drawerlayout设置为rootView的代码:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        myGestureDetector=new GestureDetectorCompat(this,this);
        rootView=new DrawerLayout(this);
        DrawerLayout.LayoutParams lp=new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT);
        getRootView().setPadding(0,0,0,0);
        rootView.setLayoutParams(lp);
        setContentView(rootView,lp);
        init(); //i made this function

        ViewGroup container=(ViewGroup)getLayoutInflater().inflate(myLayout,null);
//myLayout is evaluated in init(), a function i created to set the CONTAINER of the app, that is, the first child after the drawlayout.

        ViewGroup.LayoutParams lp2=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        container.setLayoutParams(lp2);

        container.setPadding(0,0,0,0);
        rootView.addView(container);

0 个答案:

没有答案