如何通过代码创建滑动面板

时间:2015-06-19 09:36:22

标签: android layout

 final Activity activity = MainActivity.this;
FrameLayout.LayoutParams fp = new FrameLayout.LayoutParams(
        FrameLayout.LayoutParams.MATCH_PARENT,
        FrameLayout.LayoutParams.MATCH_PARENT);

FrameLayout frame = new FrameLayout(activity);

RelativeLayout.LayoutParams dateArea = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, 90);
dateArea.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

RelativeLayout layout = new RelativeLayout(activity);

//SLINDING MENU

final SlidingPaneLayout sp = new SlidingPaneLayout(this);
RelativeLayout.LayoutParams slindlayoutparam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT);

LinearLayout.LayoutParams linear = new LinearLayout.LayoutParams(
        360,
        LinearLayout.LayoutParams.MATCH_PARENT);

LinearLayout ln = new LinearLayout(this);

RelativeLayout.LayoutParams leftButtonParam2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT);

myList = new ListView(this);

ln.addView(myList,linear);
sp.addView(ln,leftButtonParam2);

// Button

RelativeLayout.LayoutParams btnKnodParam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnKnodParam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
btnKnodParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
btnKnodParam.setMargins(0, 0, 0, 83);
ImageView btnKnod = new ImageView(this);
btnKnod.setImageResource(R.drawable.ic_launcher);

layout.addView(btnKnod, btnKnodParam);

frame.addView(layout, fp);
frame.addView(sp, slindlayoutparam);
addContentView(frame, fp);



sp.setPanelSlideListener(panelListener);
sp.setParallaxDistance(200);

btnKnod.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Log.e("TEST", "CLICKED");
        if(sp.isOpen()){
            sp.closePane();
        }
        else{
            sp.openPane();

        }
    }
});

我想创建一个滑动面板,通过单击按钮从左到右打开。我上面的代码工作正常,但问题是它始终从一开始就显示面板,我无法通过按钮点击打开或关闭它。请帮我修复此代码。谢谢!

1 个答案:

答案 0 :(得分:0)

为什么你不使用谷歌Design Support Library? 有一个名为NavigationView的抽屉,很容易配置。

NavigationView