PopupWindow 使用详解
程序员Android
共 3329字,需浏览 7分钟
· 2020-12-17
和你一起终身学习,这里是程序员Android
经典好文推荐,通过阅读本文,您将收获以下知识点:
一、PopupWindow简介
二、PopupWindow 的使用方法
三、底部PopupWindow的实现
一、PopupWindow简介
在学习PopupWindow之前,我们先了解一下PopupWindow的继承关系。
PopupWindow继承关系如下:
java.lang.Object
↳
android.widget.PopupWindow
PopupWindow
是一个可以在Activity
之上显示任意View
的控件。在Android
经常使用,效果跟Dialog
效果类似,不同点在于可以控制显示的位置,比如底部显示等。
二、PopupWindow 的使用方法
主要是调用PopWindow
的构造方法,通过LayoutInflater
将Layout
转换成View
,然后将View
传递过去,既可以实现,具体可以参考PopupWindow
源码,源码路径如下:framework/base/core/java/android/widget/PopupWindow.java
PopWindows构造方法及常用方法
三、底部PopupWindow的实现
1. PopupWindow实现效果
底部Popwindow实现效果
2. PopWindow 实现类
public class PopWindowMethods extends Activity {
private View mPopView;
private PopupWindow mPopupWindow;
private Button btn_pop_ok;
private Button btn_pop_cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popwindow);
InitPopWindow();
InitView();
InitClick();
}
/**
*
*/
private void InitClick() {
// TODO Auto-generated method stub
btn_pop_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "ok", 0).show();
}
});
btn_pop_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "cancel", 0).show();
}
});
}
/**
*
*/
private void InitPopWindow() {
// TODO Auto-generated method stub
// 将布局文件转换成View对象,popupview 内容视图
mPopView = getLayoutInflater().inflate(R.layout.popwindow_layout, null);
// 将转换的View放置到 新建一个popuwindow对象中
mPopupWindow = new PopupWindow(mPopView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// 点击popuwindow外让其消失
mPopupWindow.setOutsideTouchable(true);
// mpopupWindow.setBackgroundDrawable(background);
}
/**
*
*/
private void InitView() {
btn_pop_ok = (Button) mPopView.findViewById(R.id.btn_pop_ok);
btn_pop_cancel = (Button) mPopView.findViewById(R.id.btn_pop_cancel);
}
@SuppressLint("NewApi")
public void ShowPopWindow(View view) {
if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
} else {
// 设置PopupWindow 显示的形式 底部或者下拉等
// 在某个位置显示
mPopupWindow.showAtLocation(mPopView, Gravity.BOTTOM, 0, 30);
// 作为下拉视图显示
// mPopupWindow.showAsDropDown(mPopView, Gravity.CENTER, 200, 300);
}
// Toast.makeText(
// getApplicationContext(),
// "Launcher:"
// + PackageUtils.isLauncherAPK(getApplicationContext(),
// "com.miui.home"),
// 0).show();
}
}
3. PopupWindow布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:background="@drawable/popwindow_bg" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_pop_tittle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:text="PopWindow Tittle" />
<TextView
android:id="@+id/tv_pop_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_horizontal"
android:padding="20dp"
android:text="是否要退出Popwindows ? " />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="3dp"
android:background="@color/white_line" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_pop_ok"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="@drawable/custom_btn_white_selector"
android:gravity="center"
android:text="OK" />
<Button
android:id="@+id/btn_pop_cancel"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="@drawable/custom_btn_white_selector"
android:gravity="center"
android:text="Cancel" />
LinearLayout>
LinearLayout>
RelativeLayout>
至此,本篇已结束。转载网络的文章,小编觉得很优秀,欢迎点击阅读原文,支持原创作者,如有侵权,恳请联系小编删除。同时感谢您的阅读,期待您的关注。
点个在看,方便您使用时快速查找!
评论
金融研究 | 使用Python测量关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
金融研究(更新) | 使用Python构建关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
词向量(更新) | 使用MD&A2001-2022语料训练Word2Vec模型
buTips: 公众号推送后内容只能更改一次,且只能改20字符。 如果内容出问题,或者想更新内容, 只能重复推送。 为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-03-24-load-w2v-and-expand-your-concpe
大邓和他的Python
0
光纤详解:光纤跳线如何分类,多向单模转换?
本文来自“光纤详解:光纤跳线如何分类,多向单模转换?”,光纤跳线作为光网络布线最基础的元件之一,被广泛应用于光纤链路的搭建中。如今,光纤制造商根据应用场景的不同推出众多类型的光纤跳线,如MPO/LC/SC/FC/ST光纤跳线,单工/双工光纤跳线,单模/多模光纤跳线等,它们之间各有特色,且不可替代。本
架构师技术联盟
0
管理世界2024 | 使用管理层讨论与分析测量「企业人工智能指标」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址 https://textdata.cn/blog/2024-04-19-ai-improve-firm-productivity/
大邓和他的Python
0
GPT的风也吹到了CV,详解自回归视觉模型的先驱! ImageGPT:使用图像序列训练图像 GPT模型
作者丨科技猛兽编辑丨极市平台导读 在 CIFAR-10 上,iGPT 使用 linear probing 实现了 96.3% 的精度,优于有监督的 Wide ResNet,并通过完全微调实现了 99.0% 的精度,匹配顶级监督预训练模型。本文目录1 自回归视觉模型的先驱 ImageGPT:
机器学习初学者
0
代码 | 使用 MD&A文本测量「企业不确定性感知FEPU」
Tips: 为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2024-04-25-firm-economic-policy-uncertainty/本文使用的缩写EPU 经济政策不确定性(Economic Policy Uncerta
大邓和他的Python
0
面试官:在原生input上面使用v-model和组件上面使用有什么区别?
前言面试官:vue3的v-model都用过吧,来讲讲。粉丝:v-model其实就是一个语法糖,在编译时v-model会被编译成:modelValue属性和@update:modelValue事件。一般在子组件中定义一个名为modelValue的props来接收父组件v-model传递的值,然后当子组
高级前端进阶
0