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

android自定義popwindow

發布時間:2022-06-13 14:23:37

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);

}

}

❷ android 怎麼設置popupwindow可以拉動

設置popupwindow可以拉動
public void update(int x, int y, int width, int height, boolean force) {
if (width != -1) {
mLastWidth = width;
setWidth(width);
}
if (height != -1) {
mLastHeight = height;
setHeight(height);
}
if (!isShowing() || mContentView == null) {
return;
}
WindowManager.LayoutParams p = (WindowManager.LayoutParams) mPopupView.getLayoutParams();
boolean update = force;
final int finalWidth = mWidthMode < 0 ? mWidthMode : mLastWidth;
if (width != -1 && p.width != finalWidth) {
p.width = mLastWidth = finalWidth;
update = true;
}
final int finalHeight = mHeightMode < 0 ? mHeightMode : mLastHeight;
if (height != -1 && p.height != finalHeight) {
p.height = mLastHeight = finalHeight;
update = true;
}
if (p.x != x) {
p.x = x;
update = true;
}
if (p.y != y) {
p.y = y;
update = true;
}
final int newAnim = computeAnimationResource();
if (newAnim != p.windowAnimations) {
p.windowAnimations = newAnim;
update = true;
}
final int newFlags = computeFlags(p.flags);
if (newFlags != p.flags) {
p.flags = newFlags;
update = true;
}
if (update) {
setLayoutDirectionFromAnchor();
mWindowManager.updateViewLayout(mPopupView, p);
}
}

❸ 關於android開發中popupWindow內使用ImageView的問題。

直接從網上找了段popupWindow代碼,稍微改了下,改變裡面圖片的方法跟我平時自定義dialog時差不多(pop_layout的xml就不用貼了吧,線性布局,裡面就一個imageview和一個button)


privatevoidshowPopupWindow(Viewview){

ViewcontentView=LayoutInflater.from(view.getContext()).inflate(
R.layout.pop_window,null,false);

finalImageViewimage=(ImageView)contentView.findViewById(R.id.image1);
Buttonbutton=(Button)contentView.findViewById(R.id.button1);
button.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
image.setBackgroundColor(Color.parseColor("#ff66cc"));
}
});

PopupWindowpopupWindow=newPopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
popupWindow.setTouchable(true);

popupWindow.showAsDropDown(view);
}

❹ android popwindow怎麼設置為中間彈出

RelativeLayout layout = 「popwindow所在的layout 」;
int width = 「popwindow的寬」 ;
int height = 「popwindow的高」 ;
popupWindow.showAtLocation( layout , Gravity.LEFT | Gravity.TOP , layout.getWidth()/2 - width/2, layout.getHeight()/2 - height/2);

❺ android中popupwindow怎麼把一個頁面做個彈窗

Android PopupWindow怎麼合理控制彈出位置 void showPopupWindow(View parent) { if (popupWindow == null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.group_list, null); lv_group = (ListView) view.findViewById(R.id.lvGroup); Collections.reverse(groups); GroupAdapter groupAdapter = new GroupAdapter(this, groups); lv_group.setAdapter(groupAdapter); popupWindow = new PopupWindow(view, 200, 220); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); //設置點擊屏幕其它地方彈出框消失 popupWindow.setBackgroundDrawable(new BitmapDrawable()); WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int xPos = -popupWindow.getWidth() / 2 + getCustomTitle().getCenter().getWidth() / 2; popupWindow.showAsDropDown(parent, xPos, 4); lv_group.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { loadNew(((StringItem)(groups.get(position))).getId()); if (popupWindow != null) popupWindow.dismiss(); } }); }只需要設置proupwindows的setOutsideTouchable屬性即可。 以下為示例代碼: window.showAtLocation(parent, Gravity.RIGHT Gravity.BOTTOM, 10,10);//顯示位置 第一個參數指定PopupWindow的錨點view,即依附在哪個view上。 第二個參數指定起始點 第三個參數設置以起始點的右下角為原點,向左、上各偏移的像素。 自己看下API

❻ android 自定義的popupwindow,可以寫成單例模式

可以啊。就像寫普通的java單例類一樣,構造方法私有,只能自己進行初始化,然後對外暴露一個初始化的靜態方法。高級點再加上線程鎖。

❼ 如何打造Android自定義的下拉列表框控制項

一、概述
Android中的有個原生的下拉列表控制項Spinner,但是這個控制項有時候不符合我們自己的要求,
比如有時候我們需要類似windows 或者web網頁中常見的那種下拉列表控制項,類似下圖這樣的:

這個時候只有自己動手寫一個了。其實實現起來不算很難,
本文實現的方案是採用TextView +ImageView+PopupWindow的組合方案。
先來看看我們的自己寫的控制項效果圖吧:(源碼在文章下面最後給出哈!)

二、自定義下拉列表框控制項的實現
1. 自定義控制項用到的布局文件和資源:
結果框的布局頁面:dropdownlist_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/compound"
android:background="@drawable/dropdown_bg_selector" >

<TextView
android:id="@+id/text"
android:layout_width="250dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:text="文本文字"
android:gravity="center_vertical"
android:textSize="14sp"
android:padding="5dp"
android:singleLine="true" />
<ImageView
android:id="@+id/btn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_toRightOf="@+id/text"
android:src="@drawable/dropdown"
android:padding="5dp"
android:layout_centerVertical="true"
android:gravity="center"/>
</RelativeLayout>

下拉彈窗列表布局頁面:dropdownlist_popupwindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:divider="#666666"
android:dividerHeight="1dp"
></ListView>

</LinearLayout>

selector資源文件:
dropdown_list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dropdownlist_item_press"/>
<item android:drawable="@color/dropdownlist_item"/>
</selector>

dropdown_bg_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dropdownlist_press"/>
<item android:drawable="@color/dropdownlist_bg"/>
</selector>

2. 自定義下拉列表框控制項類的實現:
我們採用了TextView+ImageView+PopupWindow的組合方案,所以我的自定義控制項需要重寫ViewGroup,由於我們已經知道了,布局方向為豎直方向,所以這里,
我直接繼承LinearLayout來寫這個控制項。具體實現代碼如下:
package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

@SuppressLint("NewApi")
/**
* 下拉列表框控制項
* @author caiming
*
*/
public class XCDropDownListView extends LinearLayout{

private TextView editText;
private ImageView imageView;
private PopupWindow popupWindow = null;
private ArrayList<String> dataList = new ArrayList<String>();
private View mView;
public XCDropDownListView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView();
}

public void initView(){
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View view = layoutInflater.inflate(R.layout.dropdownlist_view, this,true);
editText= (TextView)findViewById(R.id.text);
imageView = (ImageView)findViewById(R.id.btn);
this.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(popupWindow == null ){
showPopWindow();
}else{
closePopWindow();
}
}
});
}
/**
* 打開下拉列表彈窗
*/
private void showPopWindow() {
// 載入popupWindow的布局文件
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View contentView = layoutInflater.inflate(R.layout.dropdownlist_popupwindow, null,false);
ListView listView = (ListView)contentView.findViewById(R.id.listView);

listView.setAdapter(new XCDropDownListAdapter(getContext(), dataList));
popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(this);
}
/**
* 關閉下拉列表彈窗
*/
private void closePopWindow(){
popupWindow.dismiss();
popupWindow = null;
}
/**
* 設置數據
* @param list
*/
public void setItemsData(ArrayList<String> list){
dataList = list;
editText.setText(list.get(0).toString());
}
/**
* 數據適配器
* @author caiming
*
*/
class XCDropDownListAdapter extends BaseAdapter{

Context mContext;
ArrayList<String> mData;
LayoutInflater inflater;
public XCDropDownListAdapter(Context ctx,ArrayList<String> data){
mContext = ctx;
mData = data;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 自定義視圖
ListItemView listItemView = null;
if (convertView == null) {
// 獲取list_item布局文件的視圖
convertView = inflater.inflate(R.layout.dropdown_list_item, null);

listItemView = new ListItemView();
// 獲取控制項對象
listItemView.tv = (TextView) convertView
.findViewById(R.id.tv);

listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
// 設置控制項集到convertView
convertView.setTag(listItemView);
} else {
listItemView = (ListItemView) convertView.getTag();
}

// 設置數據
listItemView.tv.setText(mData.get(position).toString());
final String text = mData.get(position).toString();
listItemView.layout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(text);
closePopWindow();
}
});
return convertView;
}

}
private static class ListItemView{
TextView tv;
LinearLayout layout;
}

}

三、如何使用該自定義下拉列表框控制項
使用該控制項和使用普通的自帶的控制項一樣,首先需要在布局文件中引用該控制項:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.czm.xcdropdownlistview.MainActivity"
tools:ignore="MergeRootFrame" >

<com.czm.xcdropdownlistview.XCDropDownListView
android:id="@+id/drop_down_list_view"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />

</RelativeLayout>

其次,就是在代碼中使用該控制項:
package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
/**
* 使用下拉列表框控制項 示例
* @author caiming
*
*/
public class MainActivity extends Activity {

XCDropDownListView dropDownListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dropDownListView = (XCDropDownListView)findViewById(R.id.drop_down_list_view);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0;i< 6;i++){
list.add("下拉列表項"+(i+1));
}
dropDownListView.setItemsData(list);

}

}

對了,這個控制項中,我沒有實現點擊item項回調介面,這個可能對有些寫慣了回調的可能覺得少了寫什麼的感覺,有興趣的你可以自己添加相關回調操作哈,這個大家應該都會把。

❽ 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的popWindow點擊外面執行某一動作

那是看你popupwindow的大小,如果你想定義自己的點區域,你可以把popupwindow設成全屏,然後設一個半透明的背景,加上點擊監聽,最後在這個背景上畫你想要的框

❿ 怎麼設置android中的popupwindow進入和退出的動畫

首先定義顯示效果的動畫文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="120"
android:toYDelta="0"
android:ration="500" />
</set>

然後定義消失效果的動畫文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="120"
android:ration="500" />
</set>

PopupWindow的動畫顯示效果是通過setAnimationStyle(int id)方法設置的,其中id為一個style的id,所以我們要在styles.xml文件中設置一個動畫樣式:

<style name="popwin_anim_style">
<item name="android:windowEnterAnimation">@anim/menushow</item>
<item name="android:windowExitAnimation">@anim/menuhide</item>
</style>

然後在程序中為PopupWindow設置就成:

PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setAnimationStyle(R.style.popwin_anim_style);

閱讀全文

與android自定義popwindow相關的資料

熱點內容
程序員上海與北京 瀏覽:404
安卓手機的動態照片為什麼卡 瀏覽:538
ad編譯集成庫時最常見的問題 瀏覽:846
matlab微分方程編程 瀏覽:700
安卓手機如何打開esp文件 瀏覽:545
什麼app能安裝應用 瀏覽:199
手機用什麼app看電視劇電影好 瀏覽:603
導入原理圖為什麼文件夾不顯示 瀏覽:653
androidapp風格 瀏覽:209
php取伺服器url地址 瀏覽:293
linux時間調度演算法 瀏覽:769
單片機最小電路詳解 瀏覽:185
請求要求命令 瀏覽:806
電腦文件夾發微信顯示被佔用 瀏覽:295
手機怎麼看加密視頻 瀏覽:206
怎樣解壓手機es文件包 瀏覽:661
2017年學什麼編程 瀏覽:935
金融期貨pdf 瀏覽:694
程序員客棧的信息保密嗎 瀏覽:507
編程顯示器什麼意思 瀏覽:147