android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
PopupWindow可以實(shí)現(xiàn)浮層效果,主要方法有:可以自定義view,通過(guò)LayoutInflator方法;可以出現(xiàn)和退出時(shí)顯示動(dòng)畫;可以指定顯示位置等。
為了將PopupWindow的多個(gè)功能展現(xiàn)并力求用簡(jiǎn)單的代碼實(shí)現(xiàn),編寫了一個(gè)點(diǎn)擊按鈕左側(cè)彈出菜單的功能,實(shí)現(xiàn)出現(xiàn)和退出時(shí)顯示動(dòng)畫效果并點(diǎn)擊其他區(qū)域時(shí)彈出層自動(dòng)消失,效果圖如下:
源碼:
1.PopwindowOnLeftActivity.java
package com.pop.main;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.PopupWindow;
public class PopwindowOnLeftActivity extends Activity {
// 聲明PopupWindow對(duì)象的引用
private PopupWindow popupWindow;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 點(diǎn)擊按鈕彈出菜單
Button pop = (Button) findViewById(R.id.popBtn);
pop.setOnClickListener(popClick);
}
//點(diǎn)擊彈出左側(cè)菜單的顯示方式
OnClickListener popClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getPopupWindow();
// 這里是位置顯示方式,在按鈕的左下角
popupWindow.showAsDropDown(v);
// 這里可以嘗試其它效果方式,如popupWindow.showAsDropDown(v,
// (screenWidth-dialgoWidth)/2, 0);
// popupWindow.showAtLocation(findViewById(R.id.layout),
// Gravity.CENTER, 0, 0);
}
};
/**
* 創(chuàng)建PopupWindow
*/
protected void initPopuptWindow() {
// TODO Auto-generated method stub
// 獲取自定義布局文件pop.xml的視圖
View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null,
false);
// 創(chuàng)建PopupWindow實(shí)例,200,150分別是寬度和高度
popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);
// 設(shè)置動(dòng)畫效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
//點(diǎn)擊其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
// pop.xml視圖里面的控件
Button open = (Button) popupWindow_view.findViewById(R.id.open);
Button save = (Button) popupWindow_view.findViewById(R.id.save);
Button close = (Button) popupWindow_view.findViewById(R.id.close);
// pop.xml視圖里面的控件觸發(fā)的事件
// 打開(kāi)
open.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這里可以執(zhí)行相關(guān)操作
System.out.println("打開(kāi)操作");
// 對(duì)話框消失
popupWindow.dismiss();
}
});
// 保存
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這里可以執(zhí)行相關(guān)操作
System.out.println("保存操作");
popupWindow.dismiss();
}
});
// 關(guān)閉
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這里可以執(zhí)行相關(guān)操作
System.out.println("關(guān)閉操作");
popupWindow.dismiss();
}
});
}
/***
* 獲取PopupWindow實(shí)例
*/
private void getPopupWindow() {
if (null != popupWindow) {
popupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
}
主要界面
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/popBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pop_left" />
</LinearLayout>
彈出層的布局
3.pop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray">
<Button android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:text="@string/open"/>
<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:text="@string/save"/>
<Button android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:text="@string/close"/>
</LinearLayout>
value下的style文件
4.style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AnimationFade">
<!-- PopupWindow左右彈出的效果-->
<item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
<item name="android:windowExitAnimation">@anim/out_righttoleft</item>
</style>
</resources>
value下的string文件
5.string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PopwindowOnLeftActivity!</string>
<string name="app_name">PopwindowOnLeft</string>
<string name="pop_left">彈出左側(cè)菜單</string>
<string name="open">打開(kāi)</string>
<string name="save">保存</string>
<string name="close">關(guān)閉</string>
</resources>
anim目錄下的文件
出現(xiàn)時(shí)從左往右的動(dòng)畫文件
6.in_lefttoright.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定義從左向右進(jìn)入的動(dòng)畫 -->
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="500"/>
</set>
退出時(shí)從右往左消失的動(dòng)畫
7.out_righttoleft.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定義從右向左動(dòng)畫退出動(dòng)畫 -->
<translate
android:fromXDelta="0"
android:toXDelta="-100%"
android:duration="500"/>
</set>
- Android左右滑出菜單實(shí)例分析
- android底部菜單欄實(shí)現(xiàn)原理與代碼
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
- Android ListView長(zhǎng)按彈出菜單二種實(shí)現(xiàn)方式示例
- Android界面設(shè)計(jì)(APP設(shè)計(jì)趨勢(shì) 左側(cè)隱藏菜單右邊顯示content)
- Android開(kāi)發(fā)技巧之我的菜單我做主(自定義菜單)
- Android仿QQ空間底部菜單示例代碼
- android studio 的下拉菜單Spinner使用詳解
- Android實(shí)現(xiàn)原生側(cè)滑菜單的超簡(jiǎn)單方式
- Android學(xué)習(xí)之菜單的使用方法
相關(guān)文章
百度地圖實(shí)現(xiàn)小車規(guī)劃路線后平滑移動(dòng)功能
這篇文章主要介紹了百度地圖實(shí)現(xiàn)小車規(guī)劃路線后平滑移動(dòng)功能,本文是小編寫的一個(gè)demo,通過(guò)效果圖展示的非常直白,需要的朋友可以參考下2020-01-01
Android對(duì)話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
這篇文章主要介紹了Android對(duì)話框中的提醒對(duì)話框AlertDialog、日期對(duì)話框DatePickerDialog、時(shí)間對(duì)話框TimePickerDialog使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
Android實(shí)現(xiàn)直接播放麥克風(fēng)采集到的聲音
這篇文章主要介紹了Android實(shí)現(xiàn)直接播放麥克風(fēng)采集到的聲音,涉及Android音頻操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android自動(dòng)編輯文本框(AutoCompleteTextView)使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android自動(dòng)編輯文本框AutoCompleteTextView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法
這篇文章主要介紹了Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法,本文講解了RGB值轉(zhuǎn)Bitmap、Color值轉(zhuǎn)Bitmap、字節(jié)數(shù)組轉(zhuǎn)Bitmap、讀取文件轉(zhuǎn)Bitmap、讀取資源轉(zhuǎn)Bitmap、輸入流轉(zhuǎn)Bitmap等內(nèi)容,需要的朋友可以參考下2015-01-01
Android 改變圖標(biāo)原有顏色和搜索框的實(shí)例代碼
讓Android也能有iOS那么方便的圖片色調(diào)轉(zhuǎn)換,就像同一個(gè)圖標(biāo),但是有多個(gè)地方使用,并且顏色不一樣,就可以用這個(gè)方法了。 本文實(shí)現(xiàn)TextView圖片和文字居中,鍵盤搜索功能,具體實(shí)現(xiàn)代碼大家跟隨腳本之家小編看看吧2017-09-09
Android實(shí)現(xiàn)通話自動(dòng)錄音
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)通話自動(dòng)錄音,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android開(kāi)發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能示例
這篇文章主要介紹了Android開(kāi)發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能,結(jié)合實(shí)例形式分析了Android進(jìn)度條ProgressBar的具體樣式、布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03

