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實現
private class MainHandler extends Handler{
static final int MSG_VISIBLE = 1; //顯示
static final int MSG_INVISIBLE = 2; //消失
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Animation inAnima = new AlphaAnimation(0.1f, 1.0f); //代表按鈕顯示時的動畫效果。可根據需求來設定
inAnima.setDuration(1000);
Animation outAnima = new AlphaAnimation(1.0f, 0.1f);//代表按鈕消失時的動畫效果。可根據需求來設定
outAnima.setDuration(1000);
switch(msg.what)
{
case MSG_VISIBLE:
btnTop.setAnimation(inAnima);//設定顯示時動畫
btnBottom.setAnimation(inAnima);//設定顯示時動畫
btnTop.setVisibility(View.VISIBLE);//設定顯示
btnBottom.setVisibility(View.VISIBLE);//設定顯示
break;
case MSG_INVISIBLE:
btnTop.setAnimation(outAnima);//設定消失時動畫
btnBottom.setAnimation(outAnima);//設定消失時動畫
btnTop.setVisibility(View.INVISIBLE);//設定消失
btnBottom.setVisibility(View.INVISIBLE);//設定消失
break;
default:
break;
}
}
public void sendMessage(int nMsg) { //在Handler中封裝下sendMessage函數,提高代碼簡潔性
Message msg = Message.obtain();
msg.what = nMsg;
this.sendMessage(msg);
}
}
三.設定一個變數clickCount來代表該顯示還是該隱藏視圖。
private int clickCount = 0; //奇數顯示,偶數隱藏
@Override
protected void onCreate(Bundle savedInstanceState) { //建議主函數像左邊這樣,盡量簡潔
super.onCreate(savedInstanceState);
setContentView(R.layout.testintentactivity);
btnTop = (Button)this.findViewById(R.id.btn_top);
btnBottom = (Button)this.findViewById(R.id.btn_bottom);
handler = new MainHandler(); //定義handler
setListener();
}
private void setListener() {
btnTop.setOnClickListener(this);
btnBottom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
clickCount ++; //全域變數值要變
switch (v.getId()) {
case R.id.btn_top:
Toast.makeText(this, getResources().getString(R.string.top),Toast.LENGTH_SHORT).show(); //彈出Toast來測試按鈕是否擷取到了焦點
break;
case R.id.btn_bottom:
Toast.makeText(this, getResources().getString(R.string.bottom),Toast.LENGTH_SHORT).show();//彈出Toast來測試按鈕是否擷取到了焦點
break;
default:
break;
}
handler.sendMessage(MainHandler.MSG_INVISIBLE);//不管點擊那個按鈕。兩按鈕都要設定隱藏。
}
四.監聽touch螢幕,並且通過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);
}
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實現
private class MainHandler extends Handler{
static final int MSG_VISIBLE = 1; //顯示
static final int MSG_INVISIBLE = 2; //消失
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Animation inAnima = new AlphaAnimation(0.1f, 1.0f); //代表按鈕顯示時的動畫效果。可根據需求來設定
inAnima.setDuration(1000);
Animation outAnima = new AlphaAnimation(1.0f, 0.1f);//代表按鈕消失時的動畫效果。可根據需求來設定
outAnima.setDuration(1000);
switch(msg.what)
{
case MSG_VISIBLE:
btnTop.setAnimation(inAnima);//設定顯示時動畫
btnBottom.setAnimation(inAnima);//設定顯示時動畫
btnTop.setVisibility(View.VISIBLE);//設定顯示
btnBottom.setVisibility(View.VISIBLE);//設定顯示
break;
case MSG_INVISIBLE:
btnTop.setAnimation(outAnima);//設定消失時動畫
btnBottom.setAnimation(outAnima);//設定消失時動畫
btnTop.setVisibility(View.INVISIBLE);//設定消失
btnBottom.setVisibility(View.INVISIBLE);//設定消失
break;
default:
break;
}
}
public void sendMessage(int nMsg) { //在Handler中封裝下sendMessage函數,提高代碼簡潔性
Message msg = Message.obtain();
msg.what = nMsg;
this.sendMessage(msg);
}
}
三.設定一個變數clickCount來代表該顯示還是該隱藏視圖。
private int clickCount = 0; //奇數顯示,偶數隱藏
@Override
protected void onCreate(Bundle savedInstanceState) { //建議主函數像左邊這樣,盡量簡潔
super.onCreate(savedInstanceState);
setContentView(R.layout.testintentactivity);
btnTop = (Button)this.findViewById(R.id.btn_top);
btnBottom = (Button)this.findViewById(R.id.btn_bottom);
handler = new MainHandler(); //定義handler
setListener();
}
private void setListener() {
btnTop.setOnClickListener(this);
btnBottom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
clickCount ++; //全域變數值要變
switch (v.getId()) {
case R.id.btn_top:
Toast.makeText(this, getResources().getString(R.string.top),Toast.LENGTH_SHORT).show(); //彈出Toast來測試按鈕是否擷取到了焦點
break;
case R.id.btn_bottom:
Toast.makeText(this, getResources().getString(R.string.bottom),Toast.LENGTH_SHORT).show();//彈出Toast來測試按鈕是否擷取到了焦點
break;
default:
break;
}
handler.sendMessage(MainHandler.MSG_INVISIBLE);//不管點擊那個按鈕。兩按鈕都要設定隱藏。
}
四.監聽touch螢幕,並且通過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);
}