2014-01-11 20 views
0

我想製作一個下拉菜單,如狀態菜單,當活動開始時隱藏,當它被按下或滑動時,它會像下圖所示打開。我該如何製作一個隱藏下拉菜單

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

我的佈局目前在其頂部欄和滾動型對那些之間的文本..一個RelativeLayout的,我想將菜單..

我不會做這個應用程序phonegap或類似的東西,只是java和xml ..

在此先感謝

編輯: 謝謝大家的幫助!我最終做了一個FrameLayout,這個FrameLayout被翻譯成了Y,然後,當點擊時,只是上下滑動。這裏是剪切..我會留在這裏以防別人需要它。

上layout.xml

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

上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 [email protected]/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; 
     } 
    }); 
} 
+0

這個鏈接可以幫助你:) http://stackoverflow.com/questions/13377361/how-to-create- a-drop-down-list-in-android –

回答

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. 
    } 
}); 
+0

就是這樣..但是我要找的效果就像一個滑塊,而不是點擊它來打開菜單。謝謝! =) – andreffs

+0

我更新了我的答案以達到此效果。 –