安卓开发入门教程全-PopupWindow用法大全

都是一群技术宅,先给大家说一个严酷的现实吧,现在是6月份,多少人顶着大太阳在找工作,现在我们既然有不错的工作或者想通过安卓学好的,我都希望每一个人去实践,就像Android开发入门QQ群:175229978很多人一样,肯去敲代码,不嫌弃麻烦。

首先给大家介绍安卓PopupWindow,不要嫌弃我讲解的有些详细。

Android的对话框有两种: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="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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.tianjf; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.PopupWindow; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
 
public class PopupWindowDemoActivity extends Activity implements OnClickListener, 
        OnCheckedChangeListener { 
 
    private Button mbutton01; 
    private Button mbutton02; 
    private Button mbutton03; 
    private Button mbutton04; 
    private PopupWindow mPopupWindow; 
    // 屏幕的width 
    private int mScreenWidth; 
    // 屏幕的height 
    private int mScreenHeight; 
    // PopupWindow的width 
    private int mPopupWindowWidth; 
    // PopupWindow的height 
    private int mPopupWindowHeight; 
 
    @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); 
            break; 
 
        // 相对某个控件的位置(正左下方),有偏移 
        case R.id.button02: 
            getPopupWindowInstance(); 
            mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50 
            break; 
 
        // 相对于父控件的位置,无偏移 
        case R.id.button03: 
            getPopupWindowInstance(); 
            mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0); 
            break; 
 
        // 相对于父控件的位置,有偏移 
        case R.id.button04: 
            getPopupWindowInstance(); 
            mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50); 
            break; 
 
        default: 
            break; 
        } 
    } 
 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
        mPopupWindow.dismiss(); 
    } 
 
    /*
     * 获取PopupWindow实例
     */ 
    private void getPopupWindowInstance() { 
        if (null != mPopupWindow) { 
            mPopupWindow.dismiss(); 
            return; 
        } 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(); 
    } 
}

最后给安卓开发入门的新手一句忠告:切记眼高手低,否则在你的程序生涯,讲师很难走的一段路程。如果有疑问,可以加请加QQ群:175229978咨询 。看似很简单,其实要操作才知道里面的东西。

时间: 2024-07-31 14:32:07

安卓开发入门教程全-PopupWindow用法大全的相关文章

Windows XP-android环境搭建-【 潭州安卓开发入门教程全】

最近我们课堂很多人都不会搭建环境,今天我们潭州安卓的老师就讲了一下怎么Windows XP-android环境搭建,我偷偷做了下笔记,希望对咱们更多学习安卓的同学有帮助,慢慢一步步跟着操作是不会有太大问题的. 注:本教程是讲解在Windows XP下搭建安卓开发环境的,不是XP系统的朋友请绕行! 在开始搭建之前,请大家先到本人的网盘中下载所需的6个文件. 下载球球裙:175229978 安装目录: 步骤1à安装JDK---------------------------------对应的安装文件

安卓开发入门与面试题01(潭州安卓开发入门教程)

今天老师讲到安卓的就业市场,说的我都想去面试了,想看看市面上咱们到底可以拿到什么样的工资,做什么样的行业.说来惭愧,我还只是一个安卓开发的新手,今天就和大家一样,开始我的安卓开发梦想启航.这个是潭州安卓开发入门教程里面最基础的. 从来以为学习安卓的都是为了兴趣,原来还可以为了就业,可是好难,就像咱们Android开发入门视频教程QQ群:175229978里面很多人一样,连面试题是哪一些都不懂,不过今天我分享到群里了,也希望在这里可以帮助更多安卓开发入门的小伙伴们. 1.什么是Activity?

一看就懂的Android APP开发入门教程

一看就懂的Android APP开发入门教程 作者: 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Android APP开发入门教程,从SDK下载.开发环境搭建.代码编写.APP打包等步骤一一讲解,非常简明的一个Android APP开发入门教程,需要的朋友可以参考下 工作中有做过手机App项目,前端和android或ios程序员配合完成整个项目的开发,开发过程中与ios程序配合基本没什么问题,而android各种机子和rom的问题很多,这也让我产生了学习android和ios程序开发的

Arduino可穿戴开发入门教程(大学霸内部资料)

Arduino可穿戴开发入门教程(大学霸内部资料) 试读下载地址:链接:http://pan.baidu.com/s/1mg9To28 密码:z5v8 介绍:Arduino可穿戴开发入门教程(大学霸内部资料)为国内第一本可穿戴技术实施教程.本教程以最流行的开源硬件Arduino讲解可穿戴产品的构建方法.全教程覆盖Arduino Lily硬件.软件开发和项目构建等内容.最后,还讲解三个项目:番茄闹钟.二进制时钟.P#OV手*&环. 目  录 第1章  LilyPad Arduino概览 1 1.1

iOS开发入门教程_iOS开发视频教程

iOS开发入门教程 (Object-C.网络编程.多线程.蓝牙.二维码.Cocos2D.OpenGL)适合人群:初级课时数量:34课时用到技术:IOS,Object-C,OpenGL,XCode,Cocos 2D涉及项目:Cocos+2D.Game Kit蓝牙数据处理等咨询QQ:1840215592 iOS开发入门教程详细查看:http://www.ibeifeng.com/goods-471.html1.1.课程目标iOS开发入门教程内容的目标是初学者入门,让入门者提高,让所有人符合企业招聘的

微信公众平台开发入门教程

在这篇微信公众平台开发教程中,我们假定你已经有了PHP语言程序.MySQL数据库.计算机网络通讯.及HTTP/XML/CSS/JS等基础. 我们将使用微信公众账号方倍工作室作为讲解的例子,二维码见底部. 本系列教程将引导你完成如下任务: 创建新浪云计算平台应用 启用微信公众平台开发模式 基础接口消息及事件 微信公众平台PHP SDK 微信公众平台开发模式原理 开发天气预报功能 第一章 申请服务器资源 创建新浪云计算应用 申请账号 我们使用SAE新浪云计算平台作为服务器资源,并且申请PHP环境+M

安卓开发入门指南--安卓手机自适应draw9patch不失真背景设置具体步骤

一.[安卓手机自适应draw9patch不失真背景]实际问题 前一段时间,去长江玩了一趟,拍了很多照片,不过都是手机拍的,正常尺寸都是看不清楚老婆的脸蛋,就不自在的开始放大放小,可是一定程度图片就失真了.不知道你们都遇见过这样的情况吗?其实作为一个程序开发者,我很清楚这个手机自适应draw9patch不失真背景不怎么好,今天不妨就实际问题给大家解决一下. 首先背景自适应且不失真问题的存在已经给大家聚过例子了,希望大家多在生活中观察,遇到任何安卓app问题,可加QQ群:175229978进行交流.

微名汇-微信公众平台开发入门教程

在这篇微信公众平台开发教程中,我们假定你已经有了PHP语言程序.MySQL数据库.计算机网络通讯.及HTTP/XML/CSS/JS等基础. 本系列教程将引导你完成如下任务: 创建新浪云计算平台应用 启用微信公众平台开发模式 基础接口消息及事件 微信公众平台PHP SDK 微信公众平台开发模式原理 开发天气预报功能 第一章 申请服务器资源 创建新浪云计算应用 申请账号 我们使用SAE新浪云计算平台作为服务器资源,并且申请PHP环境+MySQL数据库作为程序运行环境.申请地址:http://sae.

AE开发 入门教程

AE开发 入门教程 此过程说明适合那些使用.NET建立和部署应用的开发者,它描述了使用ArcGIS控件建立和部署应用的方法和步骤. 你可以在下面的目录下找到相应的样例程序: <安装目录>/DeveloperKit/Samples/Developer_Guide_Scenarios/ ArcGIS_Engine/Building_an_ArcGIS_Control_Application/Map_Viewer 注:ArcGIS样例程序不包含在ArcGIS Engine开发工具包“典型”安装方式中