Android-自定义PopupWindow,android系统工程师面试宝典

android:src=“@drawable/ofm_collect_icon” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:layout_marginLeft=“10dp”

android:gravity=“center”

android:text=“我的收藏”

android:textColor=“@color/white”

android:textSize=“15sp” />

<TextView

android:layout_width=“fill_parent”

android:layout_height=“0.2dp”

android:background=“@color/black” />

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:padding=“8dp”

android:visibility=“gone” >

<ImageView

android:layout_width=“35dp”

android:layout_height=“35dp”

android:scaleType=“fitCenter”

android:src=“@drawable/ofm_card_icon” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:layout_marginLeft=“10dp”

android:gravity=“center”

android:text=“我的银行卡”

android:textColor=“@color/white”

android:textSize=“15sp” />

<TextView

android:layout_width=“fill_parent”

android:layout_height=“0.2dp”

android:background=“@color/black”

android:visibility=“gone” />

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:padding=“8dp” >

<ImageView

android:layout_width=“35dp”

android:layout_height=“35dp”

android:scaleType=“fitCenter”

android:src=“@drawable/ofm_setting_icon” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:layout_marginLeft=“10dp”

android:gravity=“center”

android:text=“设置”

android:textColor=“@color/white”

android:textSize=“15sp” />

<TextView

android:layout_width=“fill_parent”

android:layout_height=“0.2dp”

android:background=“@color/black” />

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:padding=“8dp” >

<ImageView

android:layout_width=“35dp”

android:layout_height=“35dp”

android:scaleType=“fitCenter”

android:src=“@drawable/ofm_blacklist_icon” />

<Button

android:id=“@+id/btn_cancel”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:layout_marginLeft=“10dp”

android:background=“@null”

android:gravity=“center”

android:text=“退出登录”

android:textColor=“@color/white”

android:textSize=“15sp” />

以上分别是主页面和两个popupWindow布局

下面自定义两个PopupWindow,自己封装自己想要的PopuoWindow,这里只是给出示例

/14_CustomPopupWindow/src/com/wwj/popupwindow/AddPopWindow.java

package com.wwj.popupwindow;

import android.app.Activity;

import android.content.Context;

import android.graphics.drawable.ColorDrawable;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.LinearLayout;

import android.widget.PopupWindow;

/**

  • 自定义popupWindow

  • @author wwj

*/

public class AddPopWindow extends PopupWindow {

private View conentView;

public AddPopWindow(final Activity context) {

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

conentView = inflater.inflate(R.layout.add_popup_dialog, null);

int h = context.getWindowManager().getDefaultDisplay().getHeight();

int w = context.getWindowManager().getDefaultDisplay().getWidth();

// 设置SelectPicPopupWindow的View

this.setContentView(conentView);

// 设置SelectPicPopupWindow弹出窗体的宽

this.setWidth(w / 2 + 50);

// 设置SelectPicPopupWindow弹出窗体的高

this.setHeight(LayoutParams.WRAP_CONTENT);

// 设置SelectPicPopupWindow弹出窗体可点击

this.setFocusable(true);

this.setOutsideTouchable(true);

// 刷新状态

this.update();

// 实例化一个ColorDrawable颜色为半透明

ColorDrawable dw = new ColorDrawable(0000000000);

// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作

this.setBackgroundDrawable(dw);

// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

// 设置SelectPicPopupWindow弹出窗体动画效果

this.setAnimationStyle(R.style.AnimationPreview);

LinearLayout addTaskLayout = (LinearLayout) conentView

.findViewById(R.id.add_task_layout);

LinearLayout teamMemberLayout = (LinearLayout) conentView

.findViewById(R.id.team_member_layout);

addTaskLayout.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

AddPopWindow.this.dismiss();

}

});

teamMemberLayout.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

AddPopWindow.this.dismiss();

}

});

}

/**

  • 显示popupWindow

  • @param parent

*/

public void showPopupWindow(View parent) {

if (!this.isShowing()) {

// 以下拉方式显示popupwindow

this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);

} else {

this.dismiss();

}

}

}

/14_CustomPopupWindow/src/com/wwj/popupwindow/MorePopWindow.java

package com.wwj.popupwindow;

import android.app.Activity;

import android.content.Context;

import android.graphics.drawable.ColorDrawable;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.PopupWindow;

public class MorePopWindow extends PopupWindow {

private View conentView;

public MorePopWindow(final Activity context) {

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

conentView = inflater.inflate(R.layout.more_popup_dialog, null);

int h = context.getWindowManager().getDefaultDisplay().getHeight();

int w = context.getWindowManager().getDefaultDisplay().getWidth();

// 设置SelectPicPopupWindow的View

this.setContentView(conentView);

// 设置SelectPicPopupWindow弹出窗体的宽

this.setWidth(w / 2 + 50);

// 设置SelectPicPopupWindow弹出窗体的高

this.setHeight(LayoutParams.WRAP_CONTENT);

// 设置SelectPicPopupWindow弹出窗体可点击

this.setFocusable(true);

this.setOutsideTouchable(true);

// 刷新状态

this.update();

// 实例化一个ColorDrawable颜色为半透明

ColorDrawable dw = new ColorDrawable(0000000000);

// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作

this.setBackgroundDrawable(dw);

// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

// 设置SelectPicPopupWindow弹出窗体动画效果

this.setAnimationStyle(R.style.AnimationPreview);

}

public void showPopupWindow(View parent) {

if (!this.isShowing()) {

this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);

} else {

this.dismiss();

}

}

}

上面用到一个动画样式效果:

/14_CustomPopupWindow/res/values/styles.xml

用到两个动画资源

/14_CustomPopupWindow/res/anim/fade_in.xml

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android=“http://schemas.android.com/apk/res/android”

android:interpolator=“@android:anim/accelerate_decelerate_interpolator”

android:fromXScale=“0.001”

android:toXScale=“1.0”

android:fromYScale=“0.001”

android:toYScale=“1.0”

android:pivotX=“100%”

android:pivotY=“10%”

android:duration=“200” />

/14_CustomPopupWindow/res/anim/fade_out.xml

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android=“http://schemas.android.com/apk/res/android”

android:interpolator=“@android:anim/accelerate_decelerate_interpolator”

android:fromXScale=“1.0”

android:toXScale=“0.001”

android:fromYScale=“1.0”

android:toYScale=“0.001”

android:pivotX=“100%”

android:pivotY=“10%”

android:duration=“200” />

/14_CustomPopupWindow/src/com/wwj/popupwindow/MainActivity.java

package com.wwj.popupwindow;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

七大模块学习资料:如NDK模块开发、Android框架体系架构…

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!

由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。如有需要获取完整的资料文档的朋友点击我的【GitHub】免费获取。

外链图片转存中…(img-alvZ4EWe-1711323363185)]

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

[外链图片转存中…(img-0niVFoJ6-1711323363185)]

七大模块学习资料:如NDK模块开发、Android框架体系架构…

[外链图片转存中…(img-GplMSKu9-1711323363186)]

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!

由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。如有需要获取完整的资料文档的朋友点击我的【GitHub】免费获取。

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您需要更高级的弹窗样式和交互时,可以使用 PopupWindow 来创建自定义布局的弹窗。下面是使用 PopupWindow 创建自定义布局的步骤: 1. 创建自定义布局文件:首先,创建一个 XML 文件来定义您的自定义布局。例如,您可以创建一个名为 `custom_popup.xml` 的文件,并在其中定义您希望显示的布局。 2. 实例化 PopupWindow:在您的 Activity 或 Fragment 中,实例化 PopupWindow 对象。 ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.custom_popup, null); PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ``` 3. 设置 PopupWindow 属性:根据需要,设置 PopupWindow 的属性,例如背景、动画效果、焦点等。 ```java popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置动画效果 popupWindow.setAnimationStyle(R.style.PopupAnimation); ``` 4. 设置布局中的控件和事件:通过 `customView` 获取布局中的控件,并设置相应的事件监听器。 ```java Button button = customView.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); ``` 5. 显示弹窗:使用 `showAtLocation()` 或 `showAsDropDown()` 方法显示弹窗。`showAtLocation()` 方法可以显示在指定的位置,而 `showAsDropDown()` 方法则可以显示在某个视图的下方。 ```java View anchorView = findViewById(R.id.anchor_view); // 锚点视图 popupWindow.showAsDropDown(anchorView); // 或者使用 showAtLocation() 方法 ``` 这样,您就可以使用 PopupWindow 创建自定义布局的弹窗。希望对您有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值