欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹

 更新時(shí)間:2013年01月24日 15:17:29   作者:  
PopupWindow可以實(shí)現(xiàn)浮層效果,主要方法有:可以自定義view,通過LayoutInflator方法;可以出現(xiàn)和退出時(shí)顯示動(dòng)畫;可以指定顯示位置等感興趣的朋友可以了解下哦,希望本文對(duì)你學(xué)習(xí)android菜單相關(guān)開發(fā)有所幫助

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

復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

<?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>

相關(guān)文章

最新評(píng)論