【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单...
生活随笔
收集整理的這篇文章主要介紹了
【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前一篇文章中有用到 PopupWindow 來實現(xiàn)彈窗的功能。簡單介紹以下吧。
官方文檔是這樣解釋的:這就是一個彈出窗口,可以用來顯示一個任意視圖。出現(xiàn)的彈出窗口是一個浮動容器的當前活動。
1.首先來個簡單的栗子,效果如下:
?
只有兩個布局文件,一個是彈窗布局(只有一張圖片),一個是主界面布局(只有一個按鈕)。
然后在主界面代碼中實例?PopupWindow ,指定彈出的界面,在按鈕點擊事件中顯示或隱藏彈窗就可以了,代碼如下:
package com.yanis.demo;import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout.LayoutParams; import android.widget.PopupWindow;public class PopupWindowActivity extends Activity {PopupWindow pop;Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_popup_window);btn = (Button) findViewById(R.id.btnShowWindow);LayoutInflater inflater = LayoutInflater.from(this);// 引入窗口配置文件 - 即彈窗的界面View view = inflater.inflate(R.layout.my_popup_window, null);// PopupWindow實例化pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, false);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (pop.isShowing()) {// 隱藏窗口,如果設(shè)置了點擊窗口外消失,則不需要此方式隱藏 pop.dismiss();} else {// 彈出窗口顯示內(nèi)容視圖,默認以錨定視圖的左下角為起點,這里為點擊按鈕 pop.showAsDropDown(v);}}});} }?
2.知道了怎么實現(xiàn) PopupWindow 彈窗,利用其特性替換系統(tǒng)自帶的菜單欄,來個仿騰訊新聞的菜單吧,效果圖如下:
?布局什么的花點時間,慢慢調(diào),自然就出來了,主要還是主界面的邏輯代碼啊,菜單就是通過 PopupWindow 來顯示的,具體代碼如下:
package com.yanis.popup_window;import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout.LayoutParams; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener,OnKeyListener {PopupWindow pop;TextView hideView;Button btnCancel;ImageView btnNight, btnWord, btnExit;View view;boolean isOut, isIn;// 是否彈窗顯示 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();}/*** 初始化組件*/private void initView() {hideView = (TextView) findViewById(R.id.hideView);LayoutInflater inflater = LayoutInflater.from(this);// 引入窗口配置文件 - 即彈窗的界面view = inflater.inflate(R.layout.menu_view, null);btnNight = (ImageView) view.findViewById(R.id.btnNight);btnWord = (ImageView) view.findViewById(R.id.btnWord);btnExit = (ImageView) view.findViewById(R.id.btnExit);btnCancel = (Button) view.findViewById(R.id.btnCancel);}/*** 初始化數(shù)據(jù)*/private void initData() {btnNight.setOnClickListener(this);btnWord.setOnClickListener(this);btnExit.setOnClickListener(this);btnCancel.setOnClickListener(this);view.setFocusableInTouchMode(true);view.setOnKeyListener(this);// PopupWindow實例化pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, true);/*** PopupWindow 設(shè)置*/// pop.setFocusable(true); //設(shè)置PopupWindow可獲得焦點// pop.setTouchable(true); //設(shè)置PopupWindow可觸摸// pop.setOutsideTouchable(true); // 設(shè)置非PopupWindow區(qū)域可觸摸// 設(shè)置PopupWindow顯示和隱藏時的動畫 pop.setAnimationStyle(R.style.MenuAnimationFade);/*** 改變背景可拉的彈出窗口。后臺可以設(shè)置為null。 這句話必須有,否則按返回鍵popwindow不能消失 或者加入這句話* ColorDrawable dw = new* ColorDrawable(-00000);pop.setBackgroundDrawable(dw);*/pop.setBackgroundDrawable(new BitmapDrawable());}/*** 按鈕點擊事件監(jiān)聽* * @param v*/@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnNight:changePopupWindowState();Toast.makeText(MainActivity.this, "你點擊了夜間模式", Toast.LENGTH_SHORT).show();break;case R.id.btnWord:changePopupWindowState();Toast.makeText(MainActivity.this, "你點擊了文本模式", Toast.LENGTH_SHORT).show();break;case R.id.btnExit:exitTheDemo();break;case R.id.btnCancel:changePopupWindowState();break;}}/*** 退出程序*/private void exitTheDemo() {changePopupWindowState();new AlertDialog.Builder(MainActivity.this).setMessage("確定退出這個 Demo 嗎?").setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {finish();}}).setNegativeButton("取消", null).show();}/*** 改變 PopupWindow 的顯示和隱藏*/private void changePopupWindowState() {if (pop.isShowing()) {// 隱藏窗口,如果設(shè)置了點擊窗口外消失,則不需要此方式隱藏 pop.dismiss();} else {// 彈出窗口顯示內(nèi)容視圖,默認以錨定視圖的左下角為起點,這里為點擊按鈕pop.showAtLocation(hideView, Gravity.BOTTOM, 0, 0);}}// Called when a key was pressed down and not handled by any of the views// inside of the activity @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_MENU:// 菜單鍵監(jiān)聽isOut = true;changePopupWindowState();break;}return super.onKeyDown(keyCode, event);}// Called when a hardware key is dispatched to a view. @Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_MENU:if (isOut && !isIn) {isOut = false;isIn = true;} else if (!isOut && isIn) {isIn = false;changePopupWindowState();}break;}return false;}}本文轉(zhuǎn)自葉超Luka博客園博客,原文鏈接:http://www.cnblogs.com/yc-755909659/p/4288864.html,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: The Changing Face of
- 下一篇: 第十七章 apache 性能调优