android popup菜单Window控件的大小问题。

求助:PopupWindow在自定义布局里面动态添加控件问题
[问题点数:40分,结帖人mlwise]
求助:PopupWindow在自定义布局里面动态添加控件问题
[问题点数:40分,结帖人mlwise]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。Android中的PopupWindow详解 - 冬天的守候 - 51Testing软件测试网 51Testing软件测试网-中国软件测试人的精神家园
度过了一个冬眠,在春末夏初之际,补充自己的营养,努力学习,努力工作!
Android中的PopupWindow详解
& 17:17:32
/ 个人分类:
的对话框有两种: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等),可以设置偏移或无偏移下面通过一个Demo讲解(解释看注释):main.xml[html] &?xml version="1.0" encoding="utf-8"?&&&LinearLayout xmlns:android=""&&&& android:layout_width="fill_parent"&&&& android:layout_height="fill_parent"&&&& android:orientation="vertical" &&&&&& &TextView&&&&&&&& android:layout_width="fill_parent"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:text="@string/hello" /&&&&&& &Button&&&&&&&& android:id="@+id/button01"&&&&&&&& android:layout_width="fill_parent"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:text="以自己为Anchor,不偏移" /&&&&&& &Button&&&&&&&& android:id="@+id/button02"&&&&&&&& android:layout_width="fill_parent"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:text="以自己为Anchor,有偏移" /&&&&&& &Button&&&&&&&& android:id="@+id/button03"&&&&&&&& android:layout_width="fill_parent"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:text="以屏幕中心为参照,不偏移(正中间)" /&&&&&& &Button&&&&&&&& android:id="@+id/button04"&&&&&&&& android:layout_width="fill_parent"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:text="以屏幕下方为参照,下方中间" /&&&&/LinearLayout&&popup_window.xml[html]&?xml version="1.0" encoding="utf-8"?&&&LinearLayout xmlns:android=""&&&& android:layout_width="wrap_content"&&&& android:layout_height="wrap_content"&&&& android:background="#00FF00"&&&& android:orientation="vertical" &&&&&& &TextView&&&&&&&& android:layout_width="wrap_content"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:text="选择状态:"&&&&&&&& android:textColor="@android:color/white"&&&&&&&& android:textSize="20px" /&&&&&& &RadioGroup&&&&&&&& android:id="@+id/radioGroup"&&&&&&&& android:layout_width="wrap_content"&&&&&&&& android:layout_height="wrap_content"&&&&&&&& android:orientation="vertical" &&&&&&&&&& &RadioButton android:text="在线" /&&&&&&&&&& &RadioButton android:text="离线" /&&&&&&&&&& &RadioButton android:text="隐身" /&&&&& &/RadioGroup&&&&/LinearLayout&&PopupWindowDemoActivity.java[java]package com.&&import android.app.A&import android.os.B&import android.view.G&import android.view.LayoutI&import android.view.V&import android.view.View.OnClickL&import android.widget.B&import android.widget.PopupW&import android.widget.RadioG&import android.widget.RadioGroup.OnCheckedChangeL&&public class PopupWindowDemoActivity extends Activity implements OnClickListener,&&&&&&&& OnCheckedChangeListener {&&&&& private Button mbutton01;&&&& private Button mbutton02;&&&& private Button mbutton03;&&&& private Button mbutton04;&&&& private PopupWindow mPopupW&&&& // 屏幕的width&&&& private int mScreenW&&&& // 屏幕的height&&&& private int mScreenH&&&& // PopupWindow的width&&&& private int mPopupWindowW&&&& // PopupWindow的height&&&& private int mPopupWindowH&&&&& @Override&&&& public void onCreate(Bundle savedInstanceState) {&&&&&&&& super.onCreate(savedInstanceState);&&&&&&&& setContentView(R.layout.main);&&&&&&&&& mbutton01 = (Button) findViewById(R.id.button01);&&&&&&&& mbutton02 = (Button) findViewById(R.id.button02);&&&&&&&& mbutton03 = (Button) findViewById(R.id.button03);&&&&&&&& mbutton04 = (Button) findViewById(R.id.button04);&&&&&&&&& mbutton01.setOnClickListener(this);&&&&&&&& mbutton02.setOnClickListener(this);&&&&&&&& mbutton03.setOnClickListener(this);&&&&&&&& mbutton04.setOnClickListener(this);&&&& }&&&&& @Override&&&& public void onClick(View v) {&&&&&&&& switch (v.getId()) {&&&&&&&& // 相对某个控件的位置(正左下方),无偏移&&&&&&&& case R.id.button01:&&&&&&&&&&&& getPopupWindowInstance();&&&&&&&&&&&& mPopupWindow.showAsDropDown(v);&&&&&&&&&&&&&&&&&&&&& // 相对某个控件的位置(正左下方),有偏移&&&&&&&& case R.id.button02:&&&&&&&&&&&& getPopupWindowInstance();&&&&&&&&&&&& mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50&&&&&&&&&&&&&&&&&&&&& // 相对于父控件的位置,无偏移&&&&&&&& case R.id.button03:&&&&&&&&&&&& getPopupWindowInstance();&&&&&&&&&&&& mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);&&&&&&&&&&&&&&&&&&&&& // 相对于父控件的位置,有偏移&&&&&&&& case R.id.button04:&&&&&&&&&&&& getPopupWindowInstance();&&&&&&&&&&&& mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);&&&&&&&&&&&&&&&&&&&&& default:&&&&&&&&&&&&&&&&&&&& }&&&& }&&&&& @Override&&&& public void onCheckedChanged(RadioGroup group, int checkedId) {&&&&&&&& mPopupWindow.dismiss();&&&& }&&&&& /*&&&& * 获取PopupWindow实例&&&& */&&&& private void getPopupWindowInstance() {&&&&&&&& if (null != mPopupWindow) {&&&&&&&&&&&& mPopupWindow.dismiss();&&&&&&&&&&&&&&&&&&&& } else {&&&&&&&&&&&& initPopuptWindow();&&&&&&&& }&&&& }&&&&& /*&&&& * 创建PopupWindow&&&& */&&&& private void initPopuptWindow() {&&&&&&&& LayoutInflater layoutInflater = LayoutInflater.from(this);&&&&&&&& View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);&&&&&&&& RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);&&&&&&&& radioGroup.setOnCheckedChangeListener(this);&&&&&&&&& // 创建一个PopupWindow&&&&&&&& // 参数1:contentView 指定PopupWindow的内容&&&&&&&& // 参数2:width 指定PopupWindow的width&&&&&&&& // 参数3:height 指定PopupWindow的height&&&&&&&& mPopupWindow = new PopupWindow(popupWindow, 100, 130);&&&&&&&&& // 获取屏幕和PopupWindow的width和height&&&&&&&& mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();&&&&&&&& mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();&&&&&&&& mPopupWindowWidth = mPopupWindow.getWidth();&&&&&&&& mPopupWindowHeight = mPopupWindow.getHeight();&&&& }&}&&1733人阅读
Android学习(101)
作者同类文章X
在Android中有很多级别的Window,不同级别的Window按照z-index方向分布。下面看看Android控件(view)PopupWindow的用法(位置、动画、焦点)。
1、创建PopouWindow及相关参数设置
&//创建一个包含自定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx) {
&&& PopupW
&&& window = new PopupWindow(cx);
&&& TextView contentView = new TextView(cx);
&&& contentView.setGravity(Gravity.CENTER);
&&& final Resources res = cx.getResources();
&&& // contentView.setBackgroundColor(R.color.page_window_bgcolor);
&&& // window.setBackgroundDrawable(new
&&& // ColorDrawable(res.getColor(R.color.page_window_bgcolor)));
&&& contentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
&&&&&&&&&&& LayoutParams.WRAP_CONTENT));
&&& //设置PopupWindow显示和隐藏时的动画
&&& window.setAnimationStyle(R.style.AnimationFade);
&&& //设置PopupWindow的大小(宽度和高度)
&&& window.setWidth(res.getDimensionPixelSize(R.dimen.page_window_width));
&&& window.setHeight(res.getDimensionPixelSize(R.dimen.page_window_height));
&&& //设置PopupWindow的内容view
&&& window.setContentView(contentView);
&&&&&&& //设置PopupWindow外部区域是否可触摸
&&&&&&& window.setOutsideTouchable(true);
2、PopupWindow显示和隐藏的动画设置
XML配置代码:
&&!-- PopupWindow窗口淡入淡出动画 --&
&style name=&AnimationFade&&
&&& &item name=&android:windowEnterAnimation&&@anim/fade_in&/item&
&&& &item name=&android:windowExitAnimation&&@anim/fade_out&/item&
JAVA代码:
&window.setAnimationStyle(R.style.AnimationFade);
3、PopupWindow的焦点设置
&window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
4、PopupWindow的显示及显示位置设置
&window.showAtLocation();
window.showAsDropDown();
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:114585次
积分:2043
积分:2043
排名:第10787名
原创:85篇
转载:43篇
评论:19条
(1)(1)(11)(17)(4)(7)(3)(4)(1)(4)(2)(1)(9)(15)(4)(3)(7)(23)(8)(3)popupwindow Android自定义控件下拉框按钮实现,对于初学者可 看系在
238万源代码下载-
&文件名称: popupwindow
& & & & &&]
&&所属分类:
&&开发工具: Java
&&文件大小: 821 KB
&&上传时间:
&&下载次数: 3
&&提 供 者:
&详细说明:Android自定义控件下拉框按钮实现,对于初学者可以看看系在-Android popupWindow
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&下拉框&&......\.classpath&&......\.project&&......\.settings&&......\.........\org.eclipse.jdt.core.prefs&&......\AndroidManifest.xml&&......\assets&&......\bin&&......\...\AndroidManifest.xml&&......\...\classes&&......\...\.......\android&&......\...\.......\.......\support&&......\...\.......\.......\.......\v7&&......\...\.......\.......\.......\..\appcompat&&......\...\.......\.......\.......\..\.........\R$anim.class&&......\...\.......\.......\.......\..\.........\R$attr.class&&......\...\.......\.......\.......\..\.........\R$bool.class&&......\...\.......\.......\.......\..\.........\R$color.class&&......\...\.......\.......\.......\..\.........\R$dimen.class&&......\...\.......\.......\.......\..\.........\R$drawable.class&&......\...\.......\.......\.......\..\.........\R$id.class&&......\...\.......\.......\.......\..\.........\R$integer.class&&......\...\.......\.......\.......\..\.........\R$layout.class&&......\...\.......\.......\.......\..\.........\R$string.class&&......\...\.......\.......\.......\..\.........\R$style.class&&......\...\.......\.......\.......\..\.........\R$styleable.class&&......\...\.......\.......\.......\..\.........\R.class&&......\...\.......\com&&......\...\.......\...\felixzr&&......\...\.......\...\.......\popupwindow&&......\...\.......\...\.......\...........\BuildConfig.class&&......\...\.......\...\.......\...........\MainActivity$1.class&&......\...\.......\...\.......\...........\MainActivity$MyAdapter$1.class&&......\...\.......\...\.......\...........\MainActivity$MyAdapter.class&&......\...\.......\...\.......\...........\MainActivity$ViewHolder.class&&......\...\.......\...\.......\...........\MainActivity.class&&......\...\.......\...\.......\...........\R$anim.class&&......\...\.......\...\.......\...........\R$attr.class&&......\...\.......\...\.......\...........\R$bool.class&&......\...\.......\...\.......\...........\R$color.class&&......\...\.......\...\.......\...........\R$dimen.class&&......\...\.......\...\.......\...........\R$drawable.class&&......\...\.......\...\.......\...........\R$id.class&&......\...\.......\...\.......\...........\R$integer.class&&......\...\.......\...\.......\...........\R$layout.class&&......\...\.......\...\.......\...........\R$string.class&&......\...\.......\...\.......\...........\R$style.class&&......\...\.......\...\.......\...........\R$styleable.class&&......\...\.......\...\.......\...........\R.class&&......\...\jarlist.cache&&......\...\R.txt&&......\...\res&&......\gen&&......\...\android&&......\...\.......\support&&......\...\.......\.......\v7&&......\...\.......\.......\..\appcompat&&......\...\.......\.......\..\.........\R.java&&......\...\com&&......\...\...\felixzr&&......\...\...\.......\popupwindow&&......\...\...\.......\...........\BuildConfig.java&&......\...\...\.......\...........\R.java&&......\ic_launcher-web.png&&......\libs&&......\....\android-support-v4.jar&&......\proguard-project.txt&&......\project.properties&&......\res&&......\...\drawable-hdpi&&......\...\.............\delete.png&&......\...\.............\down_arrow.png&&......\...\.............\ic_launcher.png&&......\...\.............\listview_background.9.png&&......\...\.............\user.png&&......\...\drawable-ldpi&&......\...\drawable-mdpi&&......\...\.............\ic_launcher.png&&......\...\drawable-xhdpi&&......\...\..............\ic_launcher.png&&......\...\drawable-xxhdpi&&......\...\...............\ic_launcher.png&&......\...\layout&&......\...\......\activity_main.xml&&......\...\......\item_lv.xml&&......\...\values&&......\...\values-v11&&......\...\..........\styles.xml&&......\...\values-v14&&......\...\..........\styles.xml&&......\...\......\strings.xml&&......\...\......\styles.xml&&......\src&&......\...\com&&......\...\...\felixzr&&......\...\...\.......\popupwindow&&......\...\...\.......\...........\MainActivity.java
&输入关键字,在本站238万海量源码库中尽情搜索:
&[] - 缓存策略实现功能,图片加载添加缓存,智慧小区物业管理实现
&[] - NDK开发中常见的错误,帮助初学者容易进行分析总结的错误

我要回帖

更多关于 android popup菜单 的文章

 

随机推荐