Android中还是会经经常使用到Popupwindow。一种类似于对话框风格的窗体,当然类似于对话框风格也能够用Activity,能够參考:Android中使用Dialog风格弹出框的Activity

一般使用Popupwindow创建对话框风格的窗体仅仅须要两部:

(1)调用Popupwindow的构造函数创建Popupwindow对象,比如

PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

(2)调用Popupwindow的showAsDropDown(View v)将Popupwindow作为v组件的下拉组件显示出来;或者调用Popupwindow的showAtLocation()方法将Popupwindow在指定位置显示。两者能够一起设置



以下就要開始动手啦

首先在activity_main.xml布局文件里加入设置button,用于当点击这个button时弹出Popupwindow-对话框风格的窗体

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/activity_tabhost_height"
android:background="@color/blue"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="探索Popupwindow"
android:textColor="@color/white"
android:textSize="@dimen/acitvity_title_size" /> <TextView
android:id="@+id/tv_set"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="设置"
android:textColor="@color/white"
android:textSize="@dimen/acitvity_title_size" />
</RelativeLayout>
</LinearLayout>

然后再创建一个叫activity_set.xml的布局文件。用于点击设置button时载入将显示对话框风格的窗体的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="wrap_content"
android:background="@color/white"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="上班时间:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" /> <Button
android:id="@+id/tv_signin_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:text="9:00"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
</LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="下班时间:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" /> <Button
android:id="@+id/tv_signout_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:text="18:00"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
</LinearLayout>
</LinearLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin"> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="公司位置:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="5dp"
android:text="又一次定位"
android:textColor="@color/blue"
android:textSize="@dimen/size_text_medium" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin"> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="设置管理员:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:src="@mipmap/icon_toright" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin"> <Button
android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/white"
android:gravity="center"
android:text="关闭popupWindow"
android:textColor="@drawable/selector_text"
android:textSize="@dimen/size_text_medium" />
</RelativeLayout>
</LinearLayout>

接下来就是MainActivity.class程序開始的主布局文件啦

package com.xiaolijuan.popupwindowdome;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView; public class MainActivity extends Activity implements View.OnClickListener {
private TextView mTvSet; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvSet = (TextView) findViewById(R.id.tv_set); mTvSet.setOnClickListener(this);
} @Override
public void onClick(View v) {
//载入点击设置button时将显示对话框风格的窗体的XML布局文件
View root = this.getLayoutInflater().inflate(
R.layout.activity_set, null);
//创建popupWindow对象(对话框窗体)
final PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//下面拉的方式显示
popupWindow.showAsDropDown(v);
//将popupWindow显示在制定位置,在这里作为mTvSet组件的下拉组件显示出来
popupWindow.showAtLocation(findViewById(R.id.tv_set),
Gravity.CENTER, 0, 0);
root.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}

当中点击btn_exitbutton时候,使用popupWindow.dismiss()方法将Popupwindow窗体关闭

效果例如以下图:

那么如今问题来了。我仅仅能通过点击关闭button,我才干够关闭这个窗体么?假设我想点击窗体以外的地方就可以关闭窗体呢。那么也设置这样两句代码,例如以下图:

//触摸popupwindow外部,使popupWindow能够消失就必需要设置背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//不设置默认的就是False
popupWindow.setOutsideTouchable(true);

这两句代码必须设置在Popupwindow的showAsDropDown(View v)方法或者Popupwindow的showAtLocation()方法之前

完整版:

package com.xiaolijuan.popupwindowdome;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView; public class MainActivity extends Activity implements View.OnClickListener {
private TextView mTvSet; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvSet = (TextView) findViewById(R.id.tv_set); mTvSet.setOnClickListener(this);
} @Override
public void onClick(View v) {
//载入点击设置button时将显示对话框风格的窗体的XML布局文件
View root = this.getLayoutInflater().inflate(
R.layout.activity_set, null);
//创建popupWindow对象(对话框窗体)
final PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//触摸popupwindow外部,使popupWindow能够消失就必需要设置背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//不设置默认的就是False
popupWindow.setOutsideTouchable(true);
//下面拉的方式显示
popupWindow.showAsDropDown(v);
//将popupWindow显示在制定位置。在这里作为mTvSet组件的下拉组件显示出来
popupWindow.showAtLocation(findViewById(R.id.tv_set),
Gravity.CENTER, 0, 0);
root.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}

当然这一功能还能够使用Activity 的OnTouchEvent事件做到,OnTouchEvent指的是Activity
获得事件(即在PopupWindow窗体之外)

Dome下载http://download.csdn.net/detail/qq_20785431/9335251

探索Popupwindow-对话框风格的窗体(的更多相关文章

  1. 控制对话框风格的activity的显示大小与位置

    项目开发的需要,因为到现在项目接近完工,用户提出对条件筛选方式进行修改,为做到最小的改动实现用户的需求,各种百度,对于对话框风格大家普遍使用PopupWindow,但由于之前开发设计时使用的是acti ...

  2. Ribbon_窗体_实现Ribbon风格的窗体

    Ribbon_窗体_实现Ribbon风格的窗体 随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢? ...

  3. delphi 实现Ribbon风格的窗体

    随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢?本文就是为大家解开这个疑团的. 首先,Delph ...

  4. 【转】实现Ribbon风格的窗体

    随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢?本文就是为大家解开这个疑团的. 首先,Delph ...

  5. popUpWindow 动画无法超出窗体的解决方案

    popupWindow 做动画时,当要求有一个放大动画时,动画无法超出窗体,给人的感觉是只有内容在放大,窗体不动. 这是由于窗口大小固定的原因,解决方案是加大popUpwindow的 大小. 一个比较 ...

  6. android修改HOLO对话框风格

    andriod中修改对话框的风格,可以通过设置theme来实现,部分元素需要通过Java代码来修改,下面以修改对话框的标题为例说明各步骤. 1.编写一个文本样式. DIALOG的标题是一个textvi ...

  7. MFC学习笔记3---使对话框风格与系统统一

    有一件郁闷了我很久的事情,在VS中编辑对话框或者点击预览时都是以Win7风格体现的按钮及对话框: 点击上图测试对话框: 然而生成的应用程序却是这样的: 这样人很不爽啊,按钮风格回到了N年前的版本,复古 ...

  8. 自己定义android 4.0以上的对话框风格

    做个笔记.这里是Dialog的风格,假设是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...

  9. 自定义android 4.0以上的对话框风格

    做个笔记,这里是Dialog的风格,如果是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...

随机推荐

  1. Git的状态转换

             近期公司用Git来管理代码,用起来是要比svn爽一些.就是刚接触的时候比較痛苦,特别是那些状态(版本号的提交/合并/回退).差点把我搞晕了. 如今回过头来总结一下,就清楚多了.   ...

  2. 从0x00到0xFF的含义以及二进制到10进制的转换(转)

    转载自: http://www.cnblogs.com/brice/p/5343322.html 对于二进制来说,8位二进制我们称之为一个字节,二进制的表达范围值是从0b00000000-0b1111 ...

  3. [Angular] Introduction to Angular Internationalization (i18n)

    To add translation to the application: <button (click)="onEditCourse()" i18n>Edit bu ...

  4. EffectiveJava(30) -- 全面解析enum类型

    --在大多数项目中,我们会经常使用int类型来声明final类型的常量,它在不考虑安全的情况下确实能满足我们绝大多数的需求.但是在JDK1.5版本发布之后,声明一组固定的常量组成合法值的类型就建议使用 ...

  5. 【ACM】Fighting for HDU

    #include <stdio.h> #include <stdlib.h> #define max 100 /* run this program using the con ...

  6. DNS域名解析服务

    一.DNS的体系结构: DNS:域名解析系统 DNS由根域.顶级域和子域构成.根域主要负责管理顶级域,顶级域主要负责管理其下面子域. .代表DNS的根域. .com..edu等代表顶级域. shou. ...

  7. Win7如何修改文件夹的默认视图,如何把详细信息改为平铺视图

    先任意进入一个文件夹,右击选择平铺视图.   然后点击左上角的组织,文件夹和搜索选项,在文件夹选项的查看中点击"应用到文件夹",然后点击确定,弹出对话框,再确定.   随后再浏览别 ...

  8. Java学习笔记3、变量、数据类型

    标识符 常见的命名规则(见名知意) 包名全部小写 类或者接口,一个单词:首字母大写,多个单词:每个单词首字母大写. 方法或者变量:一个单词:首字母小写,多个单词:从第二个单词开始,每个单词首字母大写. ...

  9. windows下安装UNO,配置AEROO_REPORT (Openoffice4已经升级为Python2.7.5版)

    来自:http://shine-it.net/index.php?topic=8019.msg22007 最近单位要上一个OE,但OE7一天一个新更新,不知何年到头. 闲着没事写一点心得,不敢称为教程 ...

  10. 如何从官网下载Spring

    1.Spring下载地址http://repo.spring.io/release/org/springframework/spring/ 里面有各自版本下载: 方法二: 1.在百度中输入Spring ...