本文将为您提供关于Android开发实现popupWindow弹出窗口自定义布局与位置控制方法的详细介绍,我们还将为您解释androidpopwindow在指定位置弹出的相关知识,同时,我们还将为您提
本文将为您提供关于Android开发实现popupWindow弹出窗口自定义布局与位置控制方法的详细介绍,我们还将为您解释android popwindow在指定位置弹出的相关知识,同时,我们还将为您提供关于android PopupWindow 和 Activity弹出窗口实现方式、Android PopupWindow 实现自定义菜单弹窗效果、Android PopupWindow实现右侧、左侧和底部弹出菜单、Android PopupWindow弹不出来(已解决)的实用信息。
本文目录一览:- Android开发实现popupWindow弹出窗口自定义布局与位置控制方法(android popwindow在指定位置弹出)
- android PopupWindow 和 Activity弹出窗口实现方式
- Android PopupWindow 实现自定义菜单弹窗效果
- Android PopupWindow实现右侧、左侧和底部弹出菜单
- Android PopupWindow弹不出来(已解决)
Android开发实现popupWindow弹出窗口自定义布局与位置控制方法(android popwindow在指定位置弹出)
本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下:
布局文件:
主布局文件:activity_main:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bige.MainActivity" android:orientation="vertical"> <LinearLayout android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:textSize="20dp" android:paddingTop="10dp" android:gravity="left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:text="富乃宝山" android:background="#AAAAAA"/> <Button android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:background="#AAAAAA" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0点"/> <Button android:id="@+id/num1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@android:color/white" android:background="#000000" android:text="1"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ビ`ル" android:background="#A4A4A4" android:layout_marginRight="3dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="FD" android:background="#A4A4A4" android:layout_marginRight="3dp" /> <Button android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="酎" android:background="#6FA5DB"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="日本酒" android:background="#A4A4A4"/> </LinearLayout> <LinearLayout android:layout_marginTop="2dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ソフト " android:background="#A4A4A4" android:layout_marginRight="3dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="サワ` " android:background="#A4A4A4" android:layout_marginRight="3dp" /> <Button android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="カクテル " android:background="#A4A4A4"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ワイン " android:background="#A4A4A4"/> </LinearLayout> <LinearLayout android:layout_marginTop="2dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="果g酒 " android:background="#A4A4A4" android:layout_marginRight="3dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ウイスキ` " android:background="#A4A4A4" android:layout_marginRight="3dp" /> <Button android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4"/> </LinearLayout> <LinearLayout android:layout_marginTop="2dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="会席M行 " android:background="#A4A4A4" android:layout_marginRight="3dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="パックM行 " android:background="#A4A4A4" android:layout_marginRight="3dp" /> <Button android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="g品 " android:background="#A4A4A4"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_marginTop="2dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ソフト " android:background="#A4A4A4" android:layout_marginRight="3dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="サワ` " android:background="#A4A4A4" android:layout_marginRight="3dp" /> <Button android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="カクテル " android:background="#A4A4A4"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ワイン " android:background="#A4A4A4"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" android:text="ロック " /> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" android:text="水割り " /> <Button android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" android:text="お瞍 " /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" android:text="ソ`ダ割 " /> </LinearLayout> <LinearLayout android:layout_marginTop="3dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> </LinearLayout> <LinearLayout android:layout_marginTop="3dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> </LinearLayout> <LinearLayout android:layout_marginTop="3dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> </LinearLayout> <LinearLayout android:layout_marginTop="3dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" android:text=" " /> <Button android:layout_marginRight="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_marginRight="3dp" android:layout_marginLeft="3dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#A4A4A4" /> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_marginTop="10dp" android:layout_marginBottom="0dp" android:background="#5399D9" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:layout_marginLeft="15dp" android:layout_marginRight="3dp" android:background="#FFC727" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="AC"/> <Button android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:layout_marginRight="3dp" android:background="#6DB040" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="NG"/> <Button android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:layout_marginRight="3dp" android:background="#6DB040" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="後"/> <Button android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:layout_marginRight="3dp" android:background="#6DB040" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="急"/> <Button android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:layout_marginRight="3dp" android:background="#6DB040" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="▲"/> <Button android:layout_marginBottom="3dp" android:layout_marginTop="3dp" android:layout_marginRight="15dp" android:background="#FFC727" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="_J"/> </LinearLayout> </LinearLayout>
弹出布局设置:
number:
<?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" android:id="@+id/number" android:background="#FCFCFC"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:gravity="right" android:background="@drawable/edittext" android:layout_width="match_parent" android:layout_height="40dp" android:hint="0" android:textSize="35dp" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" /> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" /> <Button android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" /> </LinearLayout> <LinearLayout android:layout_marginTop="1dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" /> <Button android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" /> <Button android:layout_marginRight="1dp" android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="→" /> <Button android:background="@drawable/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/button" android:text="" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/button" android:text="OK" /> </LinearLayout> </LinearLayout>
MainActivity布局:
package com.example.bige; import android.support.v4.widget.PopupWindowCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button mbtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestwindowFeature(Window.FEATURE_NO_TITLE); getwindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); View root = this.getLayoutInflater().inflate(R.layout.number,null); //设置弹出布局自适应大小 final PopupWindow popup = new PopupWindow(root,WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT); mbtn = (Button) findViewById(R.id.num1); mbtn.setonClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // popup.isShowing(); Toast.makeText(MainActivity.this,"hh",Toast.LENGTH_SHORT).show(); //控制pupup弹出位置在父布局的中间显示 //四个参数分别表示,要参考的控件view,相对位置,后边两个参数int x,int y表示偏移 popup.showAtLocation(view,Gravity.CENTER,0); } }); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android窗口相关操作技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
android PopupWindow 和 Activity弹出窗口实现方式
本人小菜一个。目前只见过两种弹出框的实现方式,第一种是最常见的PopupWindow,第二种也就是Activity的方式是前几天才见识过。感觉很霸气哦。没想到,activity也可以做伪窗口。先贴上最常见的方法,主要讲activity的方法。
一、弹出PopupWindow
/**
* 弹出menu菜单
*/
public void menu_press(){
if(!menu_display){
//获取LayoutInflater实例
inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
//这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵
//该方法返回的是一个View的对象,是布局中的根
layout = inflater.inflate(R.layout.main_menu,null);
//下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单
menuWindow = new PopupWindow(layout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); //后两个参数是width和height
//menuWindow.showAsDropDown(layout); //设置弹出效果
//menuWindow.showAsDropDown(null,layout.getHeight());
//设置如下四条信息,当点击其他区域使其隐藏,要在show之前配置
menuWindow.setFocusable(true);
menuWindow.setoutsidetouchable(true);
menuWindow.update();
menuWindow.setBackgroundDrawable(new BitmapDrawable());
mClose = (LinearLayout)layout.findViewById(R.id.menu_close);
menuWindow.showAtLocation(this.findViewById(R.id.schoolmain),Gravity.BottOM|Gravity.CENTER_HORIZONTAL,50); //设置layout在PopupWindow中显示的位置
//如何获取我们main中的控件呢?也很简单
mMainbtn = (LinearLayout)layout.findViewById(R.id.menu_main_btn);
mHistorybtn = (LinearLayout) layout.findViewById(R.id.menu_history_btn);
mHelpbtn = (LinearLayout) layout.findViewById(R.id.menu_help_btn);
//下面对每一个Layout进行单击事件的注册吧。。。
//比如单击某个MenuItem的时候,他的背景色改变
//事先准备好一些背景图片或者颜色
mMainbtn.setonClickListener (new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mywebView.loadUrl(URL);
menuWindow.dismiss(); //响应点击事件之后关闭Menu
}
});
mHelpbtn.setonClickListener (new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mywebView.loadUrl(URL);
menuWindow.dismiss(); //响应点击事件
}
});
mHistorybtn.setonClickListener (new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mywebView.loadUrl(URL);
menuWindow.dismiss(); //响应点击事件
}
});
menu_display = true;
}else{
//如果当前已经为显示状态,则隐藏起来
menuWindow.dismiss();
menu_display = false;
}
}
public void back_press(){
if(menu_display){ //如果 Menu已经打开 ,先关闭Menu
menuWindow.dismiss();
menu_display = false;
}
else {
Intent intent = new Intent();
intent.setClass(MainActivity.this,Exit.class);
startActivity(intent);
}
}
这种方法很简单,要注意的是如果要想点击其他地方使其隐藏,要在show设置四条属性如下:
// 使其聚焦
mPopupWindow.setFocusable(true);
// 设置允许在外点击消失
mPopupWindow.setoutsidetouchable(true);
//刷新状态
mPopupWindow.update();
//点back键和其他地方使其消失,设置了这个才能触发Ondismisslistener ,设置其他控件变化等操作
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
二、Activity做伪弹窗
先贴上java代码――Exit.java
package cn.buaa.myweixin;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Exit extends Activity {
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exit_dialog);
layout=(LinearLayout)findViewById(R.id.exit_layout);
layout.setonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Todo Auto-generated method stub
Toast.makeText(getApplicationContext(),"提示:点击窗口外部关闭窗口!",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event){
finish();
return true;
}
public void exitbutton1(View v) {
this.finish();
}
public void exitbutton0(View v) {
this.finish();
MainWeixin.instance.finish();//关闭Main 这个Activity
}
}
附上布局文件:exit_dialog.xml
View Code
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/exit_layout"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@drawable/confirm_dialog_bg2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:padding="5dp"
android:textColor="#333"
android:textSize="20sp"
android:text="退出微信" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:layout_marginTop="1dp"
android:padding="10dp"
android:textSize="16sp"
android:gravity="center_horizontal"
android:text="退出后,你将收不到新的消\n息.确定要退出?" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:layout_marginBottom="8dp"
>
<Button
android:id="@+id/exitBtn0"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="是"
android:textSize="16sp"
android:textColor="#fff"
android:background="@drawable/btn_style_green"
android:gravity="center"
android:onClick="exitbutton0"
/>
<Button
android:id="@+id/exitBtn1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="否"
android:textSize="16sp"
android:textColor="#333"
android:background="@drawable/btn_style_white"
android:gravity="center"
android:onClick="exitbutton1"
/>
</LinearLayout>
</LinearLayout>