android同時彈出頂部和底部菜單
在android開發中會碰到這樣的需求,要同時彈出頂部和底部的菜單。目前已經上市的APP中有91熊貓讀書和QQ閱讀器帶這樣的功能。
點擊Menu和點擊屏幕都會彈出菜單。有很多方法可以實現。我的方法是在RelativaLayout中設置好菜單布局,然后在監聽事件中使其
顯示/隱藏。具體做法如下:
一:布局。可根據需求做一些復雜的設計。在這兒用兩個按鈕btn_top和btn_bottom。
<Button android:id="@+id/btn_top" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#AAAAAA" android:text="@string/top" android:textSize="30dip" android:textColor="#FF0000" android:visibility="invisible" /> <!-- 默認隱藏--> <Button android:id="@+id/btn_bottom" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="#AAAAAA" android:text="@string/bottom" android:textSize="30dip" android:textColor="#FF0000" android:layout_alignParentBottom="true" android:visibility="invisible" /> <!-- 默認隱藏-->
//這兩個視圖放在最上一層。在同一個layout設置需要的主布局。
二:在代碼中設置handler.因為主線程不能操作UI,只能通過handler實現
1 private class MainHandler extends Handler{ 2 static final int MSG_VISIBLE = 1; //顯示 3 static final int MSG_INVISIBLE = 2; //消失 4 @Override 5 public void handleMessage(Message msg) { 6 super.handleMessage(msg); 7 Animation inAnima = new AlphaAnimation(0.1f, 1.0f); //代表按鈕顯示時的動畫效果。可根據需求來設置 8 inAnima.setDuration(1000); 9 Animation outAnima = new AlphaAnimation(1.0f, 0.1f);//代表按鈕消失時的動畫效果。可根據需求來設置 10 outAnima.setDuration(1000); 11 switch(msg.what) 12 { 13 case MSG_VISIBLE: 14 btnTop.setAnimation(inAnima);//設置顯示時動畫 15 btnBottom.setAnimation(inAnima);//設置顯示時動畫 16 btnTop.setVisibility(View.VISIBLE);//設置顯示 17 btnBottom.setVisibility(View.VISIBLE);//設置顯示 18 break; 19 case MSG_INVISIBLE: 20 btnTop.setAnimation(outAnima);//設置消失時動畫 21 btnBottom.setAnimation(outAnima);//設置消失時動畫 22 btnTop.setVisibility(View.INVISIBLE);//設置消失 23 btnBottom.setVisibility(View.INVISIBLE);//設置消失 24 break; 25 default: 26 break; 27 } 28 } 29 30 public void sendMessage(int nMsg) { //在Handler中封裝下sendMessage函數,提高代碼簡潔性 31 Message msg = Message.obtain(); 32 msg.what = nMsg; 33 this.sendMessage(msg); 34 } 35 }
三.onCreate函數,onCreate函數盡量簡潔,能封裝出去的就封裝出去,然后調用就可以了。設置一個變量
clickCount來代表該顯示還是該隱藏視圖。
1 private int clickCount = 0; //奇數顯示,偶數隱藏 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { //建議主函數像左邊這樣 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.testintentactivity); 7 btnTop = (Button)this.findViewById(R.id.btn_top); 8 btnBottom = (Button)this.findViewById(R.id.btn_bottom); 9 handler = new MainHandler(); //定義handler 10 setListener(); 11 } 12 13 private void setListener() { 14 btnTop.setOnClickListener(this); 15 btnBottom.setOnClickListener(this); 16 } 17 18 @Override 19 public void onClick(View v) { 20 clickCount ++; //全局變量值要變 21 switch (v.getId()) { 22 case R.id.btn_top: 23 Toast.makeText(this, getResources().getString(R.string.top),Toast.LENGTH_SHORT).show(); //彈出Toast來測試按鈕是否獲取到了焦點 24 break; 25 case R.id.btn_bottom: 26 Toast.makeText(this, getResources().getString(R.string.bottom),Toast.LENGTH_SHORT).show();//彈出Toast來測試按鈕是否獲取到了焦點 27 break; 28 default: 29 break; 30 } 31 handler.sendMessage(MainHandler.MSG_INVISIBLE);//不管點擊哪個按鈕。兩按鈕都要設置隱藏。 32 }
四.通過onTouchEvent來監聽屏幕的點擊,並且通過onKeyDown監聽Menu鍵
@Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP){ clickCount++; if(clickCount % 2 == 0){ //偶數隱藏 handler.sendMessage(MainHandler.MSG_INVISIBLE); }else{ //奇數消失 handler.sendMessage(MainHandler.MSG_VISIBLE); } } return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_MENU){ //不要通過onCreateOptionsMenu來監聽Menu.它會調用系統的一些默認屬性,達不到我們想要的效果 clickCount++; if(clickCount % 2 == 0){ handler.sendMessage(MainHandler.MSG_INVISIBLE); }else{ handler.sendMessage(MainHandler.MSG_VISIBLE); } } return super.onKeyDown(keyCode, event); }
可以轉載,但請注明出處,謝謝!
作者:Carman 2012-08-13 15:27:10
郵箱:carman_loneliness@163.com