简体   繁体   中英

How can I make a drop down hidden menu

I want to make a drop down menu, like a status menu, that is hidden when the activity starts, and when it's pressed or slid it opens like the image below..

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

My layout currently has a RelativeLayout for the top bar and a ScrollView for the text.. between those, i'd like to put the menu..

I'm not doing this app on phonegap or anything like that, just java and xml..

Thanks in advance

Edit: Thank you all for your help! I end up doing a FrameLayout that was set off the screen with the translationY and then, when clicked, just slide up and down.. Here's the snipped.. I'll just leave it here in case someone else needs it.

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

Use a FrameLayout as the root layout. Add the drop menu layout as in the right side of your picture. Call

menuView.setTranslationY(-view.getHeight);

on this view to initially hide the drop down menu when the activity is started. Make sure menuView only refers to the drop down view part without the small tab button. When the user touches the tab animate translationY to 0 so that the layout will slide down

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

whereby dropDownView refers to the complete drop down menu.

Using ObjectAnimator requires API level 11. If you need to support older API levels, use http://developer.android.com/reference/android/view/animation/TranslateAnimation.html (which has some down sides).

If you instead want add a sliding effect, eg the sliding menu is moving with together with the finger, install a 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.
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM