Android学习08
PopupWindow
PopupWindow用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
1、弹出框的布局:画一个PopupWindow的布局文件
layout_pop.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tv_good"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#3CA9C4"
android:text="好"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"/> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#3CA9C4"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#3CA9C4"
android:text="一般"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"/> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#3CA9C4"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#3CA9C4"
android:text="不好"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"/> </LinearLayout>
2、activity_popup_window.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="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_pop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="POP"
android:layout_gravity="center"
android:layout_marginTop="50dp"/> </LinearLayout>
2、Activity的布局中只有一个按钮,按下后会弹出框,PopupWindowActivity代码如下:
package com.example.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast; public class PopupWindowActivity extends AppCompatActivity { private Button mBtnPop;
private PopupWindow mPop; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window); mBtnPop = findViewById(R.id.btn_pop);
mBtnPop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_pop,null);
TextView textView = view.findViewById(R.id.tv_good);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPop.dismiss();
Toast.makeText(PopupWindowActivity.this, "你选择了好", Toast.LENGTH_SHORT).show();
}
});
mPop = new PopupWindow(view,mBtnPop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
mPop.setOutsideTouchable(true);//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow
mPop.setFocusable(true);
mPop.showAsDropDown(mBtnPop,0,0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量
}
});
}
}
当有mPop.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上设置背景代码,点击外面和Back键才会消失。
mPop.setFocusable(true);
需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown();或者其它的显示PopuWindow方法之前设置它的背景不为空;
Android学习08的更多相关文章
- Android学习——windows下搭建Cygwin环境
在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- 10、android学习资源整理
1.github上整理好的开源工程 https://github.com/Trinea/android-open-project 2.最流行的android组件大全 http://colobu.com ...
- android学习系列:jercy——AI3 的博客
[android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...
- android学习一些帖子
关于谷歌和苹果的帖子 http://news.eoe.cn/18576.html android无线调试的帖子: http://baoyz.com/android/2014/06/24/adb-wir ...
- Android学习链接大放送
虽然贴链接这种事情..真是一种很偷懒的做法... 但是我一个小菜鸟,果断还是要以多向别人学习为主... 好资源要和大家分享对不对! 况且..放博客里..比收藏夹的利用几率要大一点! 原作者应该也很喜欢 ...
- Android学习路线总结,绝对干货
title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...
- Android 学习资源
下面这些资源对Android开发来说是很有帮助的! 最常用的: Android开发官方网站:http://developer.android.com/index.html 这个网站应该是Android ...
- Android学习资料收集
1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/
随机推荐
- codeforces 1285D. Dr. Evil Underscores(字典树)
链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...
- CSS遮罩层简易写法
现在很多站点弹出框,都需要一个遮罩层.写法很多,以下是我比较喜欢的一种: .box{ position:absolute; top:0; bottom:0; left:0; right:0; ba ...
- spring(三):ApplicationContext
- 2.7.1 元素定位:selenium消息框处理 (alert、confirm、prompt)
来源:http://blog.csdn.net/cui_angel/article/details/7784211 http://www.cnblogs.com/tobecrazy/p/ ...
- js获取后台map格式数据
后台: @RequestMapping("/dictList") @ResponseBody public Map<String, Object> positionLi ...
- 在电竞圈想摧枯拉朽的AI,到底能带来什么?
2019ChinaJoy,似乎并没有让这个暑期彻底燃动起来.在业界和玩家看来,2019ChinaJoy亮点匮乏.饱受诟病.不过细心观察的话,能够发现一个特殊现象--AI取代此前猎艳性质的cosplay ...
- 题解 P2320 【[HNOI2006]鬼谷子的钱袋】
P2320 [HNOI2006]鬼谷子的钱袋 挺有趣的一道题,之所以发这篇题解是因为感觉思路的更清晰一点qwq 此题主要有两种方法: 一.分治思想 例如要凑出1~20,假如我们已经能凑出1~10了,那 ...
- Object.fromEntries
//数组转换成对象 const arr = [['foo', 1],['bar', 2]] const obj = Object.fromEntries(arr) console.log(obj.ba ...
- Python开发中国象棋实战(附源码)
Pygame 做的中国象棋,一直以来喜欢下象棋,写了 python 就拿来做一个试试,水平有限,电脑走法水平低,需要在下次版本中更新电脑走法,希望源码能帮助大家更好的学习 python.总共分 ...
- typedef基本用法
[代码演示] 例一 例二 例三