如何制作下拉隐藏菜单

时间:2014-01-11 19:51:50

标签: java android xml menu

我想创建一个下拉菜单,就像状态菜单一样,在活动开始时隐藏,当按下或滑动时,它会像下面的图像一样打开..

http://i.stack.imgur.com/jeq5z.png

我的布局目前有一个顶部栏的RelativeLayout和一个ScrollView的文本..在这些之间,我想把菜单..

我没有在phonegap或类似的东西上做这个应用程序,只是java和xml ..

提前致谢

修改 的 感谢大家的帮助!我最终做了一个FrameLayout,它在屏幕上设置了translationY,然后,当点击时,只是向上和向下滑动..这是剪切的......我会把它留在这里以防其他人需要它。

layout.xml上的

<FrameLayout
    android:id="@+id/layout_FrameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ffffff" >
        <!-- stuf -->
</FrameLayout>

on activity.java

private FrameLayout statusDrawer = null;
private int statusDrawerHeight; // height of the FrameLayout (generated automatically)
private int statusDrawerDragButtonHeight = 30 + 5; //height of the DragButton + height of the border
private boolean statusDrawerOpened = false;
private int statusDrawerDuration = 750; //time in milliseconds 
private TimeInterpolator interpolator = null; //type of animation see@developer.android.com/reference/android/animation/TimeInterpolator.html

@Override
protected void onCreated(Bundle savedInstanceState){
    statusDrawer = (FrameLayout) findViewById(R.id.layout_FrameLayout);
    interpolator = new AccelerateDecelerateInterpolator();

    statusDrawer.post(new Runnable() {
        @Override
        public void run() {
            statusDrawerHeight = statusDrawer.getHeight();
            statusDrawer.setTranslationY(-statusDrawerHeight+statusDrawerDragButtonHeight);
        }
    });

    statusDrawer.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            if(statusDrawerOpened) {
                statusDrawer.animate()
                  .translationY(-statusDrawerHeight+statusDrawerDragButtonHeight)
                  .setDuration(statusDrawerDuration)
                  .setInterpolator(interpolator)
                  .start();
            } else {
                statusDrawer.animate()
                  .translationY(0)
                  .setDuration(statusDrawerDuration)
                  .setInterpolator(interpolator)
                  .start();
            }
            statusDrawerOpened = !statusDrawerOpened;
        }
    });
}

1 个答案:

答案 0 :(得分:0)

使用FrameLayout作为根布局。添加下拉菜单布局,如图片右侧所示。呼叫

menuView.setTranslationY(-view.getHeight);

在此视图上,在活动开始时最初隐藏下拉菜单。确保menuView仅指向没有小标签按钮的下拉视图部分。当用户触摸选项卡时,将translationY设置为0,以便布局向下滑动

ObjectAnimator.ofFloat(dropDownView, "translationY", -view.getHeight, 0).setDuration(200).start();

dropDownView指的是完整的下拉菜单。

使用ObjectAnimator需要API级别11.如果您需要支持较旧的API级别,请使用http://developer.android.com/reference/android/view/animation/TranslateAnimation.html(有一些不利方面)。

如果您想要添加滑动效果,例如滑动菜单与手指一起移动,安装OnTouchListener

dropDownTab.setOnTouchListener(new OnTouchListener() {
    public void onTouch(View v, MotionEvent e) {
        // Make the drop down menu finger follow the finger position.
        // Use again dropDownView.setTranslationY(...) to move the view.
        // If the drop down menu has been dragged a certain distance, make it move out by itself using the animation as above.
    }
});