Android popupwindow 演示样例程序一
经过多番測试实践,实现了popupwindow 弹出在指定控件的下方。代码上有凝视。有须要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。接下来看代码实现。
项目资源下载:点击这里
TestPopwindow2.class
package com.example.popwindowdemo; import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow; public class TestPopwindow2 extends PopupWindow {
// 根视图
private View mRootView;
// LayoutInflater
LayoutInflater mInflater; public TestPopwindow2(Activity context) {
super(context);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRootView = mInflater.inflate(R.layout.test_popwindow_2, null);
setContentView(mRootView); this.setWidth(LayoutParams.WRAP_CONTENT);
this.setHeight(LayoutParams.WRAP_CONTENT); // 设置PopUpWindow弹出的相关属性
setTouchable(true);
setOutsideTouchable(true);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable(context.getResources()));
update(); getContentView().setFocusableInTouchMode(true);
getContentView().setFocusable(true);
setAnimationStyle(R.style.AppBaseTheme);
}
}
MainActivity.class
package com.example.popwindowdemo; import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener,
OnDismissListener { private TestPopwindow2 mTestPopwindow2 = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); InitUI(); } private void InitUI() {
// 实例化TestPopwindow2
mTestPopwindow2 = new TestPopwindow2(this);
// 设置点击其它位置mTestPopwindow2消失
mTestPopwindow2.setOnDismissListener(this); Button buttonTest2 = (Button) findViewById(R.id.buttonTest2);
buttonTest2.setOnClickListener(this);
} private void OnPopwindowTest2() {
if (mTestPopwindow2 == null)
return; // location获得控件的位置
int[] location = new int[2];
View v = findViewById(R.id.buttonTest2);
if (v != null)
v.getLocationOnScreen(location); // 控件在屏幕的位置
mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme); // mTestPopwindow2弹出在某控件(button)的以下
mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT,
location[0] - v.getWidth(), location[1] + v.getHeight());
} // mTestPopwindow2布局控件的监听
public void OnclickTestListener(View view) {
switch (view.getId()) {
case R.id.layoutSeclect1:
Toast.makeText(this, "系统热门方案", Toast.LENGTH_SHORT).show();
break;
case R.id.layoutSeclect2:
Toast.makeText(this, "个人热门方案", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
if (mTestPopwindow2 != null)
mTestPopwindow2.dismiss();
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonTest2:
OnPopwindowTest2();
break;
default:
break;
}
} // 点击其它地方消失
@Override
public void onDismiss() {
}
}
test_popwindow_2.xml
<? xml version="1.0" encoding="UTF-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000" > <!--
popupwimdow的布局注意不能使用相似
android:layout_marginTop=""这样属性,特别是相对布局时候.
布局或者控件总体靠左等都会影响popupwimdow定位使用
--> <LinearLayout
android:id="@+id/hot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/title_function_bg"
android:orientation="vertical" > <LinearLayout
android:id="@+id/layoutSeclect1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="OnclickTestListener" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="系统热门方案"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:id="@+id/layoutSeclect2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="OnclickTestListener" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="个人热门方案"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout> </LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"> <Button
android:id="@+id/buttonTest2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測试2" />
</LinearLayout> </LinearLayout>
效果图例如以下
项目资源下载:点击这里
转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
如有转载未注明出处博客网址,或者没有得到作者的允许。
作者持有版权全部权。
Android popupwindow 演示样例程序一的更多相关文章
- SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...
- Tuxedo安装、配置、以及演示样例程序 (学习网址)
Tuxedo安装.配置.以及演示样例程序 (学习网址): 1.http://liu9403.iteye.com/blog/1415684 2.http://www.cnblogs.com/fnng/a ...
- Android模糊演示样例-RenderScript-附效果图与代码
本文链接 http://blog.csdn.net/xiaodongrush/article/details/31031411 參考链接 Android高级模糊技术 http://s ...
- Python Socket 编程——聊天室演示样例程序
上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket ...
- EXT-JS 6演示样例程序-Login演示样例程序
1. 用Sencha Cmd生成应用程序模版 sencha -sdk /path/to/ExtSDK generate app -classic TutorialApp./Tutoria ...
- Android之——流量管理程序演示样例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...
- 构造Scala开发环境并创建ApiDemos演示样例项目
从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...
- 函数指针使用演示样例(參考Linux-内核代码)
本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...
- 最简单的视音频播放演示样例7:SDL2播放RGB/YUV
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...
随机推荐
- “cvc-complex-type.2.4.a: Invalid content was found starting with element 'taglib'”错误的解决办法
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" ...
- 【Arduino】超声波模块(HC-SR04)
还好,这个模块有现成的库能够用: https://github.com/bosgood/arduino-playground/tree/master/lib/HCSR04Ultrasonic 下面仅仅 ...
- Oracle GoldenGate (ogg) 11.2.1.0.20 是最后一个支持oracle db 10g的 ogg版本号
參考原文: Oracle GoldenGate 11.2.1.0.22 Patch Set Availability (Doc ID 1669160.1) 该文章不做翻译,只摘录当中有价值的信息,例如 ...
- 构建SqlSessionFactory 的过程
1 SqlSessionFactory 的核心功能是创建 SqlSession 接口,而 SqlSessionFactory 是通过 SqlSessionFactoryBuilder 去构建. 构建步 ...
- mysql在linux7下systemd的相关配置
---- # Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. # # This program ...
- Windows里配置Apache2.2+PHP5.3+mod_fcgid运行高效的FastCGI模式
日发现win平台的Apache也可以跑FastCGI模式了,就折腾了一会.FastCGI模式是经实践证明了比传统的模块模式运行效率高很多.记录如下: 一.mod_fcgid配置说明 1.首先下载mod ...
- jQuery操作CheckBox的方法(选中,取消,取值)
jQuery操作CheckBox的方法(选中,取消,取值). 代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/ ...
- How to create PDF files in a Python/Django application using ReportLab
https://assist-software.net/blog/how-create-pdf-files-python-django-application-using-reportlab CONT ...
- jvm垃圾收集器之Throughput GC
呃.HotSpot VM的GC组老人之一Jon Masamitsu很久之前就写过blog讲解这个:https://blogs.oracle.com/jonthecollector/entry/our_ ...
- DelphiXE8FMX工程实现无边框托动(发送消息)
1.引用单元 uses Winapi.Windows, FMX.Platform.Win, Winapi.Messages; 2.发送消息 //发送系统消息SendMessage(FmxHandleT ...