Android PopupWindows使用





源码测试示例:
package com.example.popupwindown; import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showWindow(View v){
View view=getLayoutInflater().inflate(R.layout.pop, null);
Button button = (Button) view.findViewById(R.id.operate);
final TextView textview=(TextView) view.findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
/*Toast.makeText(MainActivity.this, "button is pressed",
Toast.LENGTH_SHORT).show();*/
textview.setText("变了");
}
}); PopupWindow pop=new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
pop.setAnimationStyle(android.R.style.Animation_Translucent);
pop.getBackground().setAlpha();
pop.setTouchable(true);//如果需要按钮单击事件,必须有
pop.setOutsideTouchable(true);
pop.setFocusable(true);
pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
pop.showAtLocation(v,Gravity.CENTER, ,); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
MainActivity.xml布局文件:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="43dp"
android:layout_marginTop="23dp"
android:text="弹出窗口"
android:onClick="showWindow" /> </RelativeLayout>
还需要一个pop.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" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <Button
android:id="@+id/operate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="操作" /> </LinearLayout>
运行结果:

Android PopupWindows使用的更多相关文章
- Android PopupWindows
		
今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...
 - Android高手速成--第一部分 个性化控件(View)
		
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
 - 据说年薪30万的Android程序员必须知道的帖子
		
Android中国开发精英 目前包括: Android开源项目第一篇--个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.G ...
 - Android开源项目分类汇总
		
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
 - android 很多牛叉布局github地址(转)
		
原文地址 http://blog.csdn.net/luo15309823081/article/details/41449929 点击可到达github-------https://github.c ...
 - GitHub上史上最全的Android开源项目分类汇总  (转)
		
GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...
 - !! 据说年薪30万的Android程序员必须知道事
		
http://www.th7.cn/Program/Android/201512/742423.shtml Android中国开发精英 目前包括: Android开源项目第一篇——个性化控件(View ...
 - 【Android】Android开源项目分类汇总
		
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
 - GitHub 优秀的 Android 开源项目(转)
		
今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...
 
随机推荐
- Linux用户组与用户组进阶命令
			
1.用户锁定 : passwd -l user1 2.解除用户锁定:passwd -u user1 3.用户无密码登记:passwd -d user1 4.添加到附属用户组:gpasswd -a us ...
 - google 地图层级和对应关系
			
google 地图层级和对应关系
 - hashcode与equals
			
归纳一下就是hashCode是用于查找使用的,而equals是用于比较两个对象的是否相等的.以下这段话是从别人帖子回复拷贝过来的: .hashcode是用来查找的,如果你学过数据结构就应该知道,在查找 ...
 - [算法][C]计算向量的角度
			
C 语言里 double atan2(double y,double x) 返回的是原点至点(x,y)的方位角,即与 x 轴的夹角.也可以理解为复数 x+yi 的辐角.返回值的单位为弧度,取值范围为 ...
 - android内存优化相关1
			
第一种策略,是释放显示相关的内存.这是我们针对系统APP采用的一种调优策略. 图形内容,俗称位图是非常占用内存的,针对位图,我们采用异步加载的方法,将位图内容信息和位图的状态信息分别进行存储,将内容信 ...
 - 使用KNN对MNIST数据集进行实验
			
由于KNN的计算量太大,还没有使用KD-tree进行优化,所以对于60000训练集,10000测试集的数据计算比较慢.这里只是想测试观察一下KNN的效果而已,不调参. K选择之前看过貌似最好不要超过2 ...
 - jqeury轮播
			
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
 - 关于viewport的研究
			
昨天项目中用到了适应移动端显示的viewport,一般的使用方式如下: <meta name="viewport" content="width=device-wi ...
 - A Knight's Journey                                                       分类:            POJ             搜索             2015-08-08 07:32    2人阅读    评论(0)    收藏
			
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...
 - Ubiquitous Religions                                                       分类:            POJ             2015-06-16 17:13    11人阅读    评论(0)    收藏
			
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 26678 Accepted: ...