当前位置: 首页 > news >正文

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
如有转载未注明出处博客网址,或者没有得到作者的允许。

作者持有版权全部权。



相关文章:

  • 我的朗科运维第七课
  • 正则表达式 re.findall 用法
  • Python中文件操作
  • 云计算与虚拟化的区别
  • JMM-java内存模型
  • 代码托管
  • 银行卡二元实名认证
  • 1576 最长严格上升子序列
  • 算法---两个栈实现一个队列
  • 机场打车有感
  • redis学习笔记(三):列表、集合、有序集合
  • AI创投的冰与火之歌:泡沫、跟风、短板和有钱花不出去的沮丧【转】
  • iOS 开发之 -- UDID和UUID的详解
  • WDS 三种模式
  • C++11: atomic 头文件
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • Angularjs之国际化
  • exif信息对照
  • export和import的用法总结
  • go append函数以及写入
  • golang中接口赋值与方法集
  • Java编程基础24——递归练习
  • js
  • k8s 面向应用开发者的基础命令
  • MySQL用户中的%到底包不包括localhost?
  • npx命令介绍
  • October CMS - 快速入门 9 Images And Galleries
  • React Native移动开发实战-3-实现页面间的数据传递
  • react 代码优化(一) ——事件处理
  • storm drpc实例
  • tweak 支持第三方库
  • 产品三维模型在线预览
  • 初识MongoDB分片
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 时间复杂度与空间复杂度分析
  • 网页视频流m3u8/ts视频下载
  • 译自由幺半群
  • 用 Swift 编写面向协议的视图
  • 在Mac OS X上安装 Ruby运行环境
  • ​​​​​​​​​​​​​​Γ函数
  • #ifdef 的技巧用法
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • $.ajax,axios,fetch三种ajax请求的区别
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (十六)串口UART
  • (四)鸿鹄云架构一服务注册中心
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (转)关于多人操作数据的处理策略