Poppuwindow的简单使用,android开发教程pdf

android:layout_height=“wrap_content”

android:gravity=“center”

android:padding=“10dp”

android:text=“拍照” />

<View

android:layout_width=“match_parent”

android:layout_height=“1dp”

android:background="@color/gray" />

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:gravity=“center”

android:padding=“10dp”

android:text=“取消” />

MainActivity代码

package com.wust.mypoppuwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.view.WindowManager;

import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void showPoppuwindow(View v){

initPopWindow(v);

}

private void initPopWindow(View v) {

//第一步:获取布局

View contentView = getLayoutInflater().inflate(R.layout.dialog_pupwindow, null, false);

//第二步:创建 PopupWindow

final PopupWindow popupWindow = new PopupWindow(contentView,

ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,

true);

// popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));

//第三步:显示 popupWindow

popupWindow.showAtLocation(v,Gravity.BOTTOM,0,0);

//第四步:关闭 popupWindow

contentView.postDelayed(new Runnable() {

@Override

public void run() {

popupWindow.dismiss();

}

},2000);

}

}

main_activity.xml布局

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

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

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=".MainActivity">

<Button

android:onClick=“showPoppuwindow”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“显示poppuwindow”/>

效果展示:

Poppuwindow的简单使用,android开发教程pdf

从上图中可以看到,poppuwindow 进入和退出动画很生硬,下面我们给他加点动画

添加动画

====

编写 style

编写动画 xml

// ent

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

er

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

//exit

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

设置属性

package com.wust.mypoppuwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.view.WindowManager;

import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void showPoppuwindow(View v){

initPopWindow(v);

}

private void initPopWindow(View v) {

//第一步:获取布局

View contentView = getLayoutInflater().inflate(R.layout.dialog_pupwindow, null, false);

//第二步:创建 PopupWindow

final PopupWindow popupWindow = new PopupWindow(contentView,

ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,

true);

// popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));

//第五步:设置动画

popupWindow.setAnimationStyle(R.style.popuWindowStyle);

//第三步:显示 popupWindow

popupWindow.showAtLocation(v,Gravity.BOTTOM,0,0);

//第四步:关闭 popupWindow

contentView.postDelayed(new Runnable() {

@Override

public void run() {

popupWindow.dismiss();

上一篇:Android入门教程 | DrawerLayout 侧滑栏


下一篇:Android --ViewPager组件的使用(含案例),滑动切换页面, PagerAdapter类