androidpopmenu
㈠ android 怎麼自定popupwindow設置高度無反應
使用PopupWindow可實現彈出窗口效果,,其實和AlertDialog一樣,也是一種對話框,兩者也經常混用,但是也各有特點。下面就看看使用方法。
首先初始化一個PopupWindow,指定窗口大小參數。
PopupWindow mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null),
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
也可以分開寫:
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
//自定義布局
ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
R.layout.window, null, true);
PopupWindow mPop = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
當然也可以手動設置PopupWindow大小。
mPop.setContentView(menuView );//設置包含視圖
mPop.setWidth(int )
mPop.setHeight(int )//設置彈出框大小
設置進場動畫:
mPop.setAnimationStyle(R.style.AnimationPreview);//設置動畫樣式
mPop.setOutsideTouchable(true);//這里設置顯示PopuWindow之後在外面點擊是否有效。如果為false的話,那麼點擊PopuWindow外面並不會關閉PopuWindow。當然這里很明顯只能在Touchable下才能使用。
當有mPop.setFocusable(false);的時候,說明PopuWindow不能獲得焦點,即使設置設置了背景不為空也不能點擊外面消失,只能由dismiss()消失,但是外面的View的事件還是可以觸發,back鍵也可以順利dismiss掉。當設置為popuWindow.setFocusable(true);的時候,加上下面兩行設置背景代碼,點擊外面和Back鍵才會消失。
mPop.setFocusable(true);
需要順利讓PopUpWindow dimiss(即點擊PopuWindow之外的地方此或者back鍵PopuWindow會消失);PopUpWindow的背景不能為空。必須在popuWindow.showAsDropDown(v);或者其它的顯示PopuWindow方法之前設置它的背景不為空:
mPop.setBackgroundDrawable(new ColorDrawable(0));
mPop.showAsDropDown(anchor, 0, 0);//設置顯示PopupWindow的位置位於View的左下方,x,y表示坐標偏移量
mPop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90);(以某個View為參考),表示彈出窗口以parent組件為參考,位於左側,偏移-90。
mPop.setOnDismissListenerd(new PopupWindow.OnDismissListener(){})//設置窗口消失事件
註:window.xml為布局文件
總結:
1、為PopupWindow的view布局,通過LayoutInflator獲取布局的view.如:
LayoutInflater inflater =(LayoutInflater)
this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View textEntryView = inflater.inflate(R.layout.paopao_alert_dialog, null);
2、顯示位置,可以有很多方式設置顯示方式
pop.showAtLocation(findViewById(R.id.ll2), Gravity.LEFT, 0, -90);
或者
pop.showAsDropDown(View anchor, int xoff, int yoff)
3、進出場動畫
pop.setAnimationStyle(R.style.PopupAnimation);
4、點擊PopupWindow區域外部,PopupWindow消失
this.window = new PopupWindow(anchor.getContext());
this.window.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() ==MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow.this.window.dismiss();
return true;
}
return false;
}
});
PopupWindow 自適應寬度實例
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxWidth = meathureWidthByChilds() + getPaddingLeft() + getPaddingRight(); super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth,MeasureSpec.EXACTLY),heightMeasureSpec);
}
public int meathureWidthByChilds() {
int maxWidth = 0;
View view = null;
for (int i = 0; i < getAdapter().getCount(); i++) {
view = getAdapter().getView(i, view, this);
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
if (view.getMeasuredWidth() > maxWidth){
maxWidth = view.getMeasuredWidth();
}
}
return maxWidth;
}
PopupWindow自適應布局
Android自帶的Menu菜單,常常無法滿足我們的需求,所以就只有自己寫menu菜單,通常的選擇是用PopupWindow來實現自定義的menu菜單,先看代碼,再來說明要注意的幾點:
View menuView = inflater.inflate(R.layout.menu_popwindow, null);
final PopupWindow p = new PopupWindow(mContext);
p.setContentView(menuView);
p.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
p.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
p.setAnimationStyle(R.style.MenuWindow);
p.setOnDismissListener(this);
p.setOutsideTouchable(false);
p.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
p.setFocusable(true); // 如果把焦點設置為false,則其他部份是可以點擊的,也就是說傳遞事件時,不會先走PopupWindow
mPopwindow = p;
㈡ android如何彈出一個占屏幕一半的菜單
android彈出一個占屏幕一半的菜單,可以使用popupwindow,設置彈出的xy軸的距離占據屏幕一半即可,如下代碼:
java">packagecom.example.hellopopupwindow;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.PopupWindow;
importandroid.widget.Toast;
{
privateContextmContext=null;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
Buttonbutton=(Button)findViewById(R.id.button);
button.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
showPopupWindow(view);
}
});
}
privatevoidshowPopupWindow(Viewview){
//一個自定義的布局,作為顯示的內容
ViewcontentView=LayoutInflater.from(mContext).inflate(
R.layout.pop_window,null);
//設置按鈕的點擊事件
Buttonbutton=(Button)contentView.findViewById(R.id.button1);
button.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Toast.makeText(mContext,"buttonispressed",
Toast.LENGTH_SHORT).show();
}
});
finalPopupWindowpopupWindow=newPopupWindow(contentView,
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(newOnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
Log.i("mengdd","onTouch:");
returnfalse;
//這里如果返回true的話,touch事件將被攔截
//攔截後PopupWindow的onTouchEvent不被調用,這樣點擊外部區域無法dismiss
}
});
//如果不設置PopupWindow的背景,無論是點擊外部區域還是Back鍵都無法dismiss彈框
//我覺得這里是API的一個bug
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));
//設置好參數之後再show
popupWindow.showAsDropDown(view);
}
}
㈢ 如何從屏幕底部向上滑出一個view-Android開發問答
從屏幕底部向上滑出一個view的方式,主要是使用TranslateAnimation,這個類,可以綁定一個控制項,在y軸方向,滑出一段高度,如下代碼:
packagecom.txlong;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.Button;
importandroid.widget.TextView;
{
privateAnimationmyAnimation_Translate;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalTextViewtv=(TextView)findViewById(R.id.tv);
Buttonbtn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
tv.setVisibility(View.VISIBLE);
myAnimation_Translate=newTranslateAnimation(
Animation.RELATIVE_TO_PARENT,-1,
Animation.RELATIVE_TO_PARENT,0,
Animation.RELATIVE_TO_PARENT,0,
Animation.RELATIVE_TO_PARENT,0);
myAnimation_Translate.setDuration(1000);
myAnimation_Translate.setInterpolator(AnimationUtils
.loadInterpolator(AndroidAnimationActivity.this,
android.R.anim.accelerate_decelerate_interpolator));
tv.startAnimation(myAnimation_Translate);
}
});
}
}
㈣ android 怎麼實現左側推出導航菜單
Android左側推出導航菜單可以讓Activity繼承PopupWindow類來實現的彈出窗體,布局可以根據自己定義設計。彈出效果主要使用了translate和alpha樣式實現。具體的做法是下列代碼:
第一步:設計彈出窗口xml:
Xml代碼
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_style_alert_dialog_background"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="從相冊選擇"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/btn_style_alert_dialog_cancel"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
第二步:創建SelectPicPopupWindow類繼承PopupWindow:
Java代碼
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.drawable.ColorDrawable;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.PopupWindow;
{
privateButtonbtn_take_photo,btn_pick_photo,btn_cancel;
privateViewmMenuView;
publicSelectPicPopupWindow(Activitycontext,OnClickListeneritemsOnClick){
super(context);
LayoutInflaterinflater=(LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView=inflater.inflate(R.layout.alert_dialog,null);
btn_take_photo=(Button)mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo=(Button)mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel=(Button)mMenuView.findViewById(R.id.btn_cancel);
//取消按鈕
btn_cancel.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//銷毀彈出框
dismiss();
}
});
//設置按鈕監聽
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//設置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//設置SelectPicPopupWindow彈出窗體的寬
this.setWidth(LayoutParams.FILL_PARENT);
//設置SelectPicPopupWindow彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//設置SelectPicPopupWindow彈出窗體可點擊
this.setFocusable(true);
//設置SelectPicPopupWindow彈出窗體動畫效果
this.setAnimationStyle(R.style.AnimBottom);
//實例化一個ColorDrawable顏色為半透明
ColorDrawabledw=newColorDrawable(0xb0000000);
//設置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
mMenuView.setOnTouchListener(newOnTouchListener(){
publicbooleanonTouch(Viewv,MotionEventevent){
intheight=mMenuView.findViewById(R.id.pop_layout).getTop();
inty=(int)event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
returntrue;
}
});
}
}
第三步:編寫MainActivity類實現測試:
Java代碼
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.TextView;
{
//自定義的彈出框類
;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextViewtv=(TextView)this.findViewById(R.id.text);
//把文字控制項添加監聽,點擊彈出自定義窗口
tv.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//實例化SelectPicPopupWindow
menuWindow=newSelectPicPopupWindow(MainActivity.this,itemsOnClick);
//顯示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main),Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);//設置layout在PopupWindow中顯示的位置
}
});
}
//為彈出窗口實現監聽類
=newOnClickListener(){
publicvoidonClick(Viewv){
menuWindow.dismiss();
switch(v.getId()){
caseR.id.btn_take_photo:
break;
caseR.id.btn_pick_photo:
break;
default:
break;
}
}
};
}
上述的代碼實現了從底部彈出,也可以根據PopupWindow類設置從左下部彈出。
Android的對話框有兩種:PopupWindow和AlertDialog。它們的不同點在於:
AlertDialog的位置固定,而PopupWindow的位置可以隨意
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的
PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項。具體如下
showAsDropDown(View anchor):相對某個控制項的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控制項的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相對於父控制項的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移
㈤ android微信的聊天記錄長按某聊天彈出的頁面是怎樣實現的
Android中應該是用ListView來實現聊天記錄的,長按某個記錄後,如果設置了偵聽器:
setOnItemLongClickListener(OnItemLongClickListener listener);
則listener中的方法: boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 將被調用,你可以實現這個方法,在其中創建你想要的對話框。
全屏對話框可以這樣實現:
1 如果你請求activity去創建,則showDialog(YOUR_DIALOG_ID);
然後在activity中的
@OverrideprotectedDialog onCreateDialog(int id){//all other dialog stuff (which dialog to display)//this line is what you need:
dialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,LayoutParams.FLAG_FULLSCREEN);return dialog;
}
2 如果直接創建,則調用:Dialog dialog=newDialog(this,R.style.Theme_Dark_NoTitleBar_FullScreen);
不知道有沒有說清楚。
㈥ Android如何自定義Menu
新建自定義Menu————>TabMenu.java如下:
package com.ncw;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
public class TabMenu extends PopupWindow {
private GridView gridView;
private LinearLayout mLayout;
public TabMenu(Context context, OnItemClickListener bodyClick, int colorBgTabMenu) {
super(context);
mLayout = new LinearLayout(context);
mLayout.setOrientation(LinearLayout.VERTICAL);
gridView = new GridView(context);
gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));// 選中的時候為透明色
gridView.setNumColumns(4);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridView.setVerticalSpacing(0);
gridView.setHorizontalSpacing(0);
gridView.setPadding(0, 0, 0, 0);
gridView.setGravity(Gravity.CENTER);
gridView.setOnItemClickListener(bodyClick);
mLayout.addView(gridView);
// 設置默認項
this.setContentView(mLayout);
this.setWidth(LayoutParams.FILL_PARENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 設置TabMenu菜單背景
this.setFocusable(true);// menu菜單獲得焦點 如果沒有獲得焦點menu菜單中的控制項事件無法響應
}
public void SetBodySelect(int index, int colorSelBody) {
int count = gridView.getChildCount();
for (int i = 0; i < count; i++) {
if (i != index)
((LinearLayout) gridView.getChildAt(i))
.setBackgroundColor(Color.TRANSPARENT);
}
((LinearLayout) gridView.getChildAt(index))
.setBackgroundColor(colorSelBody);
}
public void SetBodyAdapter(MenuBodyAdapter bodyAdapter) {
gridView.setAdapter(bodyAdapter);
}
/**
* 自定義Adapter,TabMenu的每個分頁的主體
*
*/
static public class MenuBodyAdapter extends BaseAdapter {
private Context mContext;
private int[] resID;
/**
* 設置TabMenu的分頁主體
*
* @param context
* 調用方的上下文
* @param resID
*/
public MenuBodyAdapter(Context context, int[] resID) {
this.mContext = context;
this.resID = resID;
}
@Override
public int getCount() {
return resID.length;
}
public Object getItem(int position) {
return makeMenyBody(position);
}
public long getItemId(int position) {
return position;
}
private LinearLayout makeMenyBody(int position) {
LinearLayout result = new LinearLayout(this.mContext);
result.setOrientation(LinearLayout.VERTICAL);
result.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
result.setPadding(0, 0, 0, 0);
ImageView img = new ImageView(this.mContext);
img.setBackgroundResource(resID[position]);
result.addView(img, new LinearLayout.LayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));
return result;
}
public View getView(int position, View convertView, ViewGroup parent) {
return makeMenyBody(position);
}
}
}
?
1
使用自定義Menu————>testTabMenu.java
package com.ncw;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class testTabMenu extends Activity {
TabMenu.MenuBodyAdapter bodyAdapter = new TabMenu.MenuBodyAdapter(this,
new int[] { R.drawable.menu_01, R.drawable.menu_02,
R.drawable.menu_03, R.drawable.menu_04 });
TabMenu tabMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabMenu = new TabMenu(this, new BodyClickEvent(), R.drawable.menu_bg);// 出現與消失的動畫
tabMenu.update();
tabMenu.SetBodyAdapter(bodyAdapter);
}
class BodyClickEvent implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
tabMenu.SetBodySelect(arg2, Color.GRAY);
Log.i("Log", " BodyClickEvent implements OnItemClickListener "
+ arg2);
}
}
@Override
/**
* 創建MENU
*/
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");// 必須創建一項
return super.onCreateOptionsMenu(menu);
}
@Override
/**
* 攔截MENU
*/
public boolean onMenuOpened(int featureId, Menu menu) {
if (tabMenu != null) {
if (tabMenu.isShowing())
tabMenu.dismiss();
else {
tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),
Gravity.BOTTOM, 0, 0);
}
}
return false;// 返回為true 則顯示系統menu
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定義Menu————張亞龍" >
</TextView>
</LinearLayout>
運行效果圖:
㈦ android 怎麼實現左側導航欄
Android左側推出導航菜單可以讓Activity繼承PopupWindow類來實現的彈出窗體,布局可以根據自己定義設計。彈出效果主要使用了translate和alpha樣式實現。具體的做法是下列代碼:
第一步:設計彈出窗口xml:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_style_alert_dialog_background"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="從相冊選擇"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/btn_style_alert_dialog_cancel"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
第二步:創建SelectPicPopupWindow類繼承PopupWindow:
Java代碼
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class SelectPicPopupWindow extends PopupWindow {
private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;
public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
//取消按鈕
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//銷毀彈出框
dismiss();
}
});
//設置按鈕監聽
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//設置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//設置SelectPicPopupWindow彈出窗體的寬
this.setWidth(LayoutParams.FILL_PARENT);
//設置SelectPicPopupWindow彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//設置SelectPicPopupWindow彈出窗體可點擊
this.setFocusable(true);
//設置SelectPicPopupWindow彈出窗體動畫效果
this.setAnimationStyle(R.style.AnimBottom);
//實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//設置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
});
}
}
第三步:編寫MainActivity類實現測試:
Java代碼
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//自定義的彈出框類
SelectPicPopupWindow menuWindow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) this.findViewById(R.id.text);
//把文字控制項添加監聽,點擊彈出自定義窗口
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//實例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
//顯示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //設置layout在PopupWindow中顯示的位置
}
});
}
//為彈出窗口實現監聽類
private OnClickListener itemsOnClick = new OnClickListener(){
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_take_photo:
break;
case R.id.btn_pick_photo:
break;
default:
break;
}
}
};
}
上述的代碼實現了從底部彈出,也可以根據PopupWindow類設置從左下部彈出。
Android的對話框有兩種:PopupWindow和AlertDialog。它們的不同點在於:
AlertDialog的位置固定,而PopupWindow的位置可以隨意
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的
PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項。具體如下
showAsDropDown(View anchor):相對某個控制項的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控制項的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相對於父控制項的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移
㈧ Android Popwindow下面的ListView的item點擊事件怎麼響應
package com.example.myselfpopupdemo;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView list;
private ImageButton popButton;
private String[] popInfo = new String[] { "測試1", "測試2 ", "測試3", "測試4" };
private PopupWindow pWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.list, null);
pWindow = new PopupWindow(v, 120,LayoutParams.WRAP_CONTENT);
pWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pWindow.setOutsideTouchable(true);
pWindow.setFocusable(true);
pWindow.update();
this.list = (ListView) v.findViewById(R.id.list);
this.popButton = (ImageButton) findViewById(R.id.btn_title_popmenu);
this.popButton.setOnClickListener(new popOnClickListenerImpl());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, R.layout.pomenu_item, popInfo);
this.list.setAdapter(adapter);
this.list.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class popOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pWindow.showAsDropDown(v);
}
}
private class OnItemClickListenerImpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
pWindow.dismiss();
Toast.makeText(MainActivity.this, ""+popInfo[position], 2000).show();
}
}
}
通過 inflater.inflate找到ListView所在的父組件,再list = (ListView) v.findViewById(R.id.list);通過這個找到ListView,之後就可以對ListView添加點擊事件!