android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
PopupWindow可以實(shí)現(xiàn)浮層效果,主要方法有:可以自定義view,通過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ā)的事件
// 打開
open.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這里可以執(zhí)行相關(guān)操作
System.out.println("打開操作");
// 對(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">打開</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開發(fā)技巧之我的菜單我做主(自定義菜單)
- Android仿QQ空間底部菜單示例代碼
- android studio 的下拉菜單Spinner使用詳解
- Android實(shí)現(xiàn)原生側(cè)滑菜單的超簡(jiǎn)單方式
- Android學(xué)習(xí)之菜單的使用方法
相關(guān)文章
Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面效果
這篇文章主要介紹了Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面效果,需要的朋友可以參考下2017-05-05Android開發(fā)MediaCodec和lamemp3多段音頻截取拼接
這篇文章主要為大家介紹了Android開發(fā)使用MediaCodec和lamemp3實(shí)現(xiàn)多段音頻截取拼接的編程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法
這篇文章主要介紹了Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳
這篇文章主要介紹了基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程
最近在做需求的時(shí)候,碰到有各種篩選項(xiàng)的界面,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Android Handler消息派發(fā)機(jī)制源碼分析
這篇文章主要為大家詳細(xì)分析了Android Handler消息派發(fā)機(jī)制源碼,感興趣的小伙伴們可以參考一下2016-07-07android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案
今天小編就為大家分享一篇關(guān)于android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03