Android-自定义PopupWindow(1)

文章详细描述了如何在Android应用中使用LinearLayout和PopupWindow创建自定义对话框,包括设置布局属性、显示和隐藏操作,以及AddPopWindow和MorePopWindow两个自定义PopupWindow类的实现。同时提到提供一套完整的Android开发学习资源以帮助开发者提升技能。
摘要由CSDN通过智能技术生成

android:gravity=“center_horizontal”

android:orientation=“vertical” >

<LinearLayout

android:id=“@+id/pop_layout2”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”

android:layout_alignParentTop=“true”

android:background=“@drawable/abc_ab_bottom_solid_dark_holo”

android:gravity=“center_horizontal”

android:orientation=“vertical” >

<LinearLayout

android:id=“@+id/add_task_layout”

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_add_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=“15dip” />

<TextView

android:layout_width=“fill_parent”

android:layout_height=“0.2dp”

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

<LinearLayout

android:id=“@+id/team_member_layout”

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_profile_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=“15dip” />

/14_CustomPopupWindow/res/layout/more_popup_dialog.xml

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

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

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:gravity=“center_horizontal”

android:orientation=“vertical” >

<LinearLayout

android:id=“@+id/pop_layout”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”

android:layout_alignParentTop=“true”

android:background=“@drawable/abc_ab_bottom_solid_dark_holo”

android:gravity=“center_horizontal”

android:orientation=“vertical” >

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:padding=“8dp” >

<ImageView

android:layout_width=“50dp”

android:layout_height=“50dp”

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

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:gravity=“left|center_horizontal”

android:orientation=“vertical”

android:padding=“5dp” >

<TextView

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“wwj”

android:textColor=“@color/white”

android:textSize=“15sp” />

<TextView

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“whatswwj”

android:textColor=“@color/green”

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_photo_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_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

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

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个学习思路及方向,从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

image

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

技术停滞不前!**

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

[外链图片转存中…(img-4TZKsgO7-1713216053574)]

[外链图片转存中…(img-vOGq4VMK-1713216053576)]

[外链图片转存中…(img-Glktco9W-1713216053577)]

[外链图片转存中…(img-6kaUu7Yh-1713216053578)]

[外链图片转存中…(img-mlPTmXIt-1713216053579)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个学习思路及方向,从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

[外链图片转存中…(img-SLph1WZA-1713216053580)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 23
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值