導航:首頁 > 操作系統 > android自定義窗口

android自定義窗口

發布時間:2024-04-20 14:52:02

『壹』 android 怎麼設置 懸浮activity

Android懸浮窗實現

下面實現來自於android學習手冊,裡面有實現的可運行的例子還有源碼。android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼
實現基礎
Android懸浮窗實現使用WindowManager ,WindowManager介紹
通過Context.getSystemService(Context.WINDOW_SERVICE)可以獲得 WindowManager對象。
每一個WindowManager對象都和一個特定的 Display綁定。
想要獲取一個不同的display的WindowManager,可以用 createDisplayContext(Display)來獲取那個display的 Context,之後再使用:Context.getSystemService(Context.WINDOW_SERVICE)來獲取WindowManager。
使用WindowManager可以在其他應用最上層,甚至手機桌面最上層顯示窗口。
調用的是WindowManager繼承自基類的addView方法和removeView方法來顯示和隱藏窗口。具體見後面的實例。
另:API 17推出了Presentation,它將自動獲取display的Context和WindowManager,可以方便地在另一個display上顯示窗口。

WindowManager實現懸浮窗需要聲明許可權
首先在manifest中添加如下許可權:
<!-- 顯示頂層浮窗 --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

注意:在MIUI上需要在設置中打開本應用的」顯示懸浮窗」開關,並且重啟應用,否則懸浮窗只能顯示在本應用界面內,不能顯示在手機桌面上。

服務獲取和基本參數設置
[java] view plain print?
// 獲取應用的Context
mContext = context.getApplicationContext();
// 獲取WindowManager
mWindowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
參數設置:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 類型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 設置flag
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果設置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件
params.flags = flags;
// 不設置這個彈出框的透明遮罩顯示為黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到後面的窗口
// 設置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時,後面的應用圖標由不可長按變為可長按
// 不設置這個flag的話,home頁的劃屏會有問題
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;

// 獲取應用的Context
mContext = context.getApplicationContext();
// 獲取WindowManager
mWindowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
//參數設置:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 類型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 設置flag
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果設置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件
params.flags = flags;
// 不設置這個彈出框的透明遮罩顯示為黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到後面的窗口
// 設置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時,後面的應用圖標由不可長按變為可長按
// 不設置這個flag的話,home頁的劃屏會有問題
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
點擊和按鍵事件
除了View中的各個控制項的點擊事件之外,彈窗View的消失控制需要一些處理。
點擊彈窗外部可隱藏彈窗的效果,首先,懸浮窗是全屏的,只不過最外層的是透明或者半透明的:

具體實現
[java] view plain print?
package com.robert.floatingwindow;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
/**
* 彈窗輔助類
*
* @ClassName WindowUtils
*
*
*/
public class WindowUtils {
private static final String LOG_TAG = "WindowUtils";
private static View mView = null;
private static WindowManager mWindowManager = null;
private static Context mContext = null;
public static Boolean isShown = false;
/**
* 顯示彈出框
*
* @param context
* @param view
*/
public static void showPopupWindow(final Context context) {
if (isShown) {
LogUtil.i(LOG_TAG, "return cause already shown");
return;
}
isShown = true;
LogUtil.i(LOG_TAG, "showPopupWindow");
// 獲取應用的Context
mContext = context.getApplicationContext();
// 獲取WindowManager
mWindowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
mView = setUpView(context);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 類型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 設置flag
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果設置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View收不到Back鍵的事件
params.flags = flags;
// 不設置這個彈出框的透明遮罩顯示為黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到後面的窗口
// 設置 FLAG_NOT_FOCUSABLE 懸浮窗口較小時,後面的應用圖標由不可長按變為可長按
// 不設置這個flag的話,home頁的劃屏會有問題
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
mWindowManager.addView(mView, params);
LogUtil.i(LOG_TAG, "add view");
}
/**
* 隱藏彈出框
*/
public static void hidePopupWindow() {
LogUtil.i(LOG_TAG, "hide " + isShown + ", " + mView);
if (isShown && null != mView) {
LogUtil.i(LOG_TAG, "hidePopupWindow");
mWindowManager.removeView(mView);
isShown = false;
}
}
private static View setUpView(final Context context) {
LogUtil.i(LOG_TAG, "setUp view");
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow,
null);
Button positiveBtn = (Button) view.findViewById(R.id.positiveBtn);
positiveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i(LOG_TAG, "ok on click");
// 打開安裝包
// 隱藏彈窗
WindowUtils.hidePopupWindow();
}
});
Button negativeBtn = (Button) view.findViewById(R.id.negativeBtn);
negativeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i(LOG_TAG, "cancel on click");
WindowUtils.hidePopupWindow();
}
});
// 點擊窗口外部區域可消除
// 這點的實現主要將懸浮窗設置為全屏大小,外層有個透明背景,中間一部分視為內容區域
// 所以點擊內容區域外部視為點擊懸浮窗外部
final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的內容區域
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
LogUtil.i(LOG_TAG, "onTouch");
int x = (int) event.getX();
int y = (int) event.getY();
Rect rect = new Rect();
popupWindowView.getGlobalVisibleRect(rect);
if (!rect.contains(x, y)) {
WindowUtils.hidePopupWindow();
}
LogUtil.i(LOG_TAG, "onTouch : " + x + ", " + y + ", rect: "
+ rect);
return false;
}
});
// 點擊back鍵可消除
view.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
WindowUtils.hidePopupWindow();
return true;
default:
return false;
}
}
});
return view;
}
}

『貳』 android使用百度地圖3.0版本怎樣實現自定義彈出窗口功能

基本原理就是用ItemizedOverlay來添加附加物,在OnTap方法中向MapView上添加一個自定義的View(如果已存在就直接設為可見),下面具體來介紹我的實現方法:


一、自定義覆蓋物類:MyPopupOverlay,這個類是最關鍵的一個類ItemizedOverlay,用於設置Marker,並定義Marker的點擊事件,彈出窗口,至於彈出窗口的內容,則通過定義Listener,放到Activity中去構造。如果沒有特殊需求,這個類不需要做什麼改動。代碼如下,popupLinear這個對象,就是加到地圖上的自定義View:

<OverlayItem>{
privateContextcontext=null;
//這是彈出窗口,包括內容部分還有下面那個小三角
=null;
//這是彈出窗口的內容部分
private
ViewpopupView=null;
privateMapViewmapView=null;
private
Projectionprojection=null;
//這是彈出窗口內容部分使用的layoutId,在Activity中設置
privateintlayoutId=
0;
//是否使用網路帶有A-J字樣的Marker
=
false;
privateint[]defaultMarkerIds={
R.drawable.icon_marka,
R.drawable.icon_markb,
R.drawable.icon_markc,
R.drawable.icon_markd,
R.drawable.icon_marke,
R.drawable.icon_markf,
R.drawable.icon_markg,
R.drawable.icon_markh,
R.drawable.icon_marki,
R.drawable.icon_markj,};
//這個Listener用於在Marker被點擊時讓Activity填充PopupView的內容
private
OnTapListeneronTapListener=null;
publicMyPopupOverlay(Contextcontext,Drawablemarker,MapViewmMapView)
{
super(marker,mMapView);
this.context=
context;
this.popupLinear=newLinearLayout(context);
this.mapView=mMapView;
popupLinear.setOrientation(LinearLayout.VERTICAL);
popupLinear.setVisibility(View.GONE);
projection=
mapView.getProjection();
}
@Override
publicbooleanonTap(GeoPointpt,MapViewmMapView)
{
//點擊窗口以外的區域時,當前窗口關閉
if(popupLinear!=null&&
popupLinear.getVisibility()==View.VISIBLE){
LayoutParamslp=
(LayoutParams)popupLinear.getLayoutParams();
PointtapP=new
Point();
projection.toPixels(pt,tapP);
PointpopP
=newPoint();
projection.toPixels(lp.point,
popP);
intxMin=popP.x-lp.width/2+lp.x;
intyMin=popP.y-lp.height+lp.y;
intxMax=popP.x+
lp.width/2+lp.x;
intyMax=popP.y+lp.y;
if
(tapP.x<xMin||tapP.y<yMin||tapP.x>xMax
||tapP.y>yMax)
popupLinear.setVisibility(View.GONE);
}
return
false;
}
@Override
protectedbooleanonTap(inti){
//
點擊Marker時,該Marker滑動到地圖中央偏下的位置,並顯示Popup窗口
OverlayItemitem=
getItem(i);
if(popupView==null){
//
如果popupView還沒有創建,則構造popupLinear
if
(!createPopupView()){
returntrue;
}
}
if(onTapListener==null)
return
true;
popupLinear.setVisibility(View.VISIBLE);
onTapListener.onTap(i,popupView);
popupLinear.measure(0,0);
intviewWidth=
popupLinear.getMeasuredWidth();
intviewHeight=
popupLinear.getMeasuredHeight();
LayoutParamslayoutParams=newLayoutParams(viewWidth,
viewHeight,
item.getPoint(),0,-60,
LayoutParams.BOTTOM_CENTER);
layoutParams.mode=
LayoutParams.MODE_MAP;
popupLinear.setLayoutParams(layoutParams);
Pointp=new
Point();
projection.toPixels(item.getPoint(),p);
p.y=
p.y-viewHeight/2;
GeoPointpoint=projection.fromPixels(p.x,
p.y);
mapView.getController().animateTo(point);
return
true;
}
privatebooleancreatePopupView(){
//TODOAuto-generated
methodstub
if(layoutId==0)
return
false;
popupView=LayoutInflater.from(context).inflate(layoutId,
null);
popupView.setBackgroundResource(R.drawable.popupborder);
ImageView
dialogStyle=newImageView(context);
dialogStyle.setImageDrawable(context.getResources().getDrawable(
R.drawable.iw_tail));
popupLinear.addView(popupView);
android.widget.LinearLayout.LayoutParamslp=new
android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
lp.topMargin=
-2;
lp.leftMargin=60;
popupLinear.addView(dialogStyle,
lp);
mapView.addView(popupLinear);
returntrue;
}
@Override
publicvoidaddItem(List<OverlayItem>items)
{
//TODOAuto-generatedmethodstub
intstartIndex=
getAllItem().size();
for(OverlayItemitem:items){
if(startIndex>=defaultMarkerIds.length)
startIndex=
defaultMarkerIds.length-1;
if(useDefaultMarker&&
item.getMarker()==null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[startIndex++]));
}
}
super.addItem(items);
}
@Override
publicvoidaddItem(OverlayItemitem){
//
TODOAuto-generatedmethodstub
//
重載這兩個addItem方法,主要用於設置自己默認的Marker
intindex=
getAllItem().size();
if(index>=
defaultMarkerIds.length)
index=defaultMarkerIds.length-
1;
if(useDefaultMarker&&item.getMarker()==
null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[getAllItem().size()]));
}
super.addItem(item);
}
publicvoidsetLayoutId(intlayoutId){
this.layoutId=
layoutId;
}
publicvoidsetUseDefaultMarker(booleanuseDefaultMarker){
this.useDefaultMarker=useDefaultMarker;
}
publicvoidsetOnTapListener(OnTapListeneronTapListener){
this.onTapListener=onTapListener;
}
publicinterfaceOnTapListener{
publicvoidonTap(intindex,
ViewpopupView);
}
}

二、MainActivity,這是主界面,用來顯示地圖,創建MyPopupOverlay對象,在使用我寫的MyPopupOverlay這個類時,需要遵循以下步驟:


創建MyPopupOverlay對象,構造函數為public MyPopupOverlay(Context context, Drawable marker, MapView mMapView),四個參數分別為當前的上下文、通用的Marker(這是ItemizedOverlay需要的,當不設置Marker時的默認Marker)以及網路地圖對象。

設置自定義的彈出窗口內容的布局文件ID,使用的方法為public void setLayoutId(int layoutId)。

設置是使用自定義的Marker,還是預先寫好的帶有A-J字樣的網路地圖原裝Marker,使用的方法為public void setUseDefaultMarker(boolean useDefaultMarker),只有當這個值為true且沒有調用OverlayItem的setMarker方法為特定點設置Marker時,才使用原裝Marker。

創建Marker所在的點,即分別創建一個個OverlayItem,然後調用public void addItem(OverlayItem item)或public void addItem(List<OverlayItem> items)方法來把這些OverlayItem添加到自定義的附加層上去。

為MyPopupOverlay對象添加onTap事件,當Marker被點擊時,填充彈出窗口中的內容(也就是第2條中layoutId布局中的內容),設置方法為public void setOnTapListener(OnTapListener onTapListener),OnTapListener是定義在MyPopupOverlay中的介面,實現這個介面需要覆寫public void onTap(int index, View popupView)方法,其中,index表示被點擊的Marker(確切地說是OverlayItem)的索引,popupView是使用layoutId這個布局的View,也就是彈出窗口除了下面的小三角之外的部分。

把這個MyPopupOverlay對象添加到地圖上去:mMapView.getOverlays().add(myOverlay);mMapView.refresh();

『叄』 android怎樣自定義dialog

Android開發過程中,常常會遇到一些需求場景——在界面上彈出一個彈框,對用戶進行提醒並讓用戶進行某些選擇性的操作,
如退出登錄時的彈窗,讓用戶選擇「退出」還是「取消」等操作。
Android系統提供了Dialog類,以及Dialog的子類,常見如AlertDialog來實現此類功能。
一般情況下,利用Android提供的Dialog及其子類能夠滿足多數此類需求,然而,其不足之處體現在:
1. 基於Android提供的Dialog及其子類樣式單一,風格上與App本身風格可能不太協調;
2. Dialog彈窗在布局和功能上有所限制,有時不一定能滿足實際的業務需求。
本文將通過在Dialog基礎上構建自定義的Dialog彈窗,以最常見的確認彈框為例。
本樣式相對比較簡單:上面有一個彈框標題(提示語),下面左右分別是「確認」和「取消」按鈕,當用戶點擊「確認」按鈕時,彈框執行
相應的確認邏輯,當點擊「取消」按鈕時,執行相應的取消邏輯。
首先,自定義彈框樣式:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content"
5 android:background="@drawable/dialog_bg"
6 android:orientation="vertical" >
7
8 <TextView
9 android:id="@+id/title"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:layout_gravity="center"
13 android:paddingTop="14dp"
14 android:textColor="@color/login_hint"
15 android:textSize="@dimen/text_size_18" />
16
17 <LinearLayout
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:layout_marginBottom="14dp"
21 android:layout_marginLeft="20dp"
22 android:layout_marginRight="20dp"
23 android:layout_marginTop="30dp" >
24
25 <TextView
26 android:id="@+id/confirm"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:layout_marginRight="10dp"
30 android:layout_weight="1"
31 android:background="@drawable/btn_confirm_selector"
32 android:gravity="center"
33 android:textColor="@color/white"
34 android:textSize="@dimen/text_size_16" />
35
36 <TextView
37 android:id="@+id/cancel"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:layout_marginLeft="10dp"
41 android:layout_weight="1"
42 android:background="@drawable/btn_cancel_selector"
43 android:gravity="center"
44 android:textColor="@color/login_hint"
45 android:textSize="@dimen/text_size_16" />
46 </LinearLayout>
47
48 </LinearLayout>

然後,通過繼承Dialog類構建確認彈框控制項ConfirmDialog:

1 package com.corn.widget;
2
3 import android.app.Dialog;
4 import android.content.Context;
5 import android.os.Bundle;
6 import android.util.DisplayMetrics;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.Window;
10 import android.view.WindowManager;
11 import android.widget.TextView;
12
13 import com.corn.R;
14
15 public class ConfirmDialog extends Dialog {
16
17 private Context context;
18 private String title;
19 private String confirmButtonText;
20 private String cacelButtonText;
21 private ClickListenerInterface clickListenerInterface;
22
23 public interface ClickListenerInterface {
24
25 public void doConfirm();
26
27 public void doCancel();
28 }
29
30 public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) {
31 super(context, R.style.MyDialog);
32 this.context = context;
33 this.title = title;
34 this.confirmButtonText = confirmButtonText;
35 this.cacelButtonText = cacelButtonText;
36 }
37
38 @Override
39 protected void onCreate(Bundle savedInstanceState) {
40 // TODO Auto-generated method stub
41 super.onCreate(savedInstanceState);
42
43 init();
44 }
45
46 public void init() {
47 LayoutInflater inflater = LayoutInflater.from(context);
48 View view = inflater.inflate(R.layout.confirm_dialog, null);
49 setContentView(view);
50
51 TextView tvTitle = (TextView) view.findViewById(R.id.title);
52 TextView tvConfirm = (TextView) view.findViewById(R.id.confirm);
53 TextView tvCancel = (TextView) view.findViewById(R.id.cancel);
54
55 tvTitle.setText(title);
56 tvConfirm.setText(confirmButtonText);
57 tvCancel.setText(cacelButtonText);
58
59 tvConfirm.setOnClickListener(new clickListener());
60 tvCancel.setOnClickListener(new clickListener());
61
62 Window dialogWindow = getWindow();
63 WindowManager.LayoutParams lp = dialogWindow.getAttributes();
64 DisplayMetrics d = context.getResources().getDisplayMetrics(); // 獲取屏幕寬、高用
65 lp.width = (int) (d.widthPixels * 0.8); // 高度設置為屏幕的0.6
66 dialogWindow.setAttributes(lp);
67 }
68
69 public void setClicklistener(ClickListenerInterface clickListenerInterface) {
70 this.clickListenerInterface = clickListenerInterface;
71 }
72
73 private class clickListener implements View.OnClickListener {
74 @Override
75 public void onClick(View v) {
76 // TODO Auto-generated method stub
77 int id = v.getId();
78 switch (id) {
79 case R.id.confirm:
80 clickListenerInterface.doConfirm();
81 break;
82 case R.id.cancel:
83 clickListenerInterface.doCancel();
84 break;
85 }
86 }
87
88 };
89
90 }

在如上空間構造代碼中,由於控制項的"確認"和"取消"邏輯與實際的應用場景有關,因此,控制項中通過定義內部介面來實現。

在需要使用此控制項的地方,進行如下形式調用:

1 public static void Exit(final Context context) {
2 final ConfirmDialog confirmDialog = new ConfirmDialog(context, "確定要退出嗎?", "退出", "取消");
3 confirmDialog.show();
4 confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
5 @Override
6 public void doConfirm() {
7 // TODO Auto-generated method stub
8 confirmDialog.dismiss();
9 //toUserHome(context);
10 AppManager.getAppManager().AppExit(context);
11 }
12
13 @Override
14 public void doCancel() {
15 // TODO Auto-generated method stub
16 confirmDialog.dismiss();
17 }
18 });
19 }

調用中實現了此控制項的內部介面,並賦給控制項本身,以此在點擊按鈕時實現基於外部具體業務邏輯的函數回調。

『肆』 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 Dialog的樣式

Android 中自定義Dialog的樣式,主要是通過自定義的xml,然後載入到dialog的背景中,如下步驟:

1、自定義Dialog

finalDialogdialog=newDialog(this,R.style.Theme_dialog);

2、窗口布局

ViewcontentView=LayoutInflater.from(this).inflate(R.layout.select_list_dialog,null);

3、把設定好的窗口布局放到dialog中

dialog.setContentView(contentView);

4、設定點擊窗口空白處取消會話

dialog.setCanceledOnTouchOutside(true);

5、具體的操作

ListViewmsgView=(ListView)contentView.findViewById(R.id.listview_flow_list);

6、展示窗口

dialog.show();
例:
finalDialogdialog=newDialog(this,R.style.Theme_dialog);
ViewcontentView=LayoutInflater.from(this).inflate(R.layout.select_list_dialog,null);
dialog.setContentView(contentView);
dialog.setCanceledOnTouchOutside(true);
ListViewmsgView=(ListView)contentView.findViewById(R.id.listview_flow_list);
TextViewtitleText=(TextView)contentView.findViewById(R.id.title);
titleText.setText("請選擇銀行卡");
=(this,mBankcardList);
msgView.setAdapter(adapter);
msgView.setOnItemClickListener(newOnItemClickListener(){
@Override
publicvoidonItemClick(AdapterViewparent,Viewview,intposition,longid){
//Toast.makeText(RechargeFlowToMobileActivity.this,
//position+"",0).show();
mSelectCard=mBankcardList.get(position);
Stringarea=mSelectCard.getBank_card();
mCardNumberText.setText(area);
dialog.dismiss();
}
});
ButtoncloseBtn=(Button)contentView.findViewById(R.id.close);
closeBtn.setClickable(true);
closeBtn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
dialog.dismiss();
}
});
dialog.show();

以上就是在Android開發自定義dialog樣式的方法和步驟,android很多的控制項都提供了介面或者方法進行樣式的定義和修改。

閱讀全文

與android自定義窗口相關的資料

熱點內容
redisphp管理 瀏覽:958
被人切掉蛋蛋電影 瀏覽:894
美國最新女機器人電影 瀏覽:22
萬達電影院用英語怎麼說 瀏覽:123
伊朗人購買加密貨幣 瀏覽:373
杭州哪兒找程序員 瀏覽:268
警察卧底監獄韓國電影叫什麼電影 瀏覽:607
app激活小米移動網路連接到伺服器地址 瀏覽:84
決策樹歸納演算法 瀏覽:873
歐美以小孩為主角的電影 瀏覽:432
肉寫得好的古言作者 瀏覽:187
韓國電影失蹤國語在線觀看 瀏覽:39
盜墓電影免費大全 瀏覽:177
內地大尺度電影 瀏覽:296
日歷女孩女二的扮演者李熙3圍 瀏覽:219
韓國電影弟弟幫哥哥找工作條件是嫂子在自己家 瀏覽:818
安卓手機怎麼重新變流暢 瀏覽:419
佑山愛 瀏覽:393
可以我的小米雲伺服器地址 瀏覽:893
血戀圖片 瀏覽:510