PopupWindow
一.PopupWindow介绍
二.如何自定义窗体
三.实现点击右上角弹出窗体
四.底部弹出窗体
五.弹出窗体背景半透明
六.设置动画
1.在res/anim文件夹下定义进场动画
2.定义进出场动画
3.为popupwindow设置动画
七.自定义PopupWindow
PopupWindow
显示方法 | 显示位置 |
---|---|
showAsDropDown(View anchor, int xoff, int yoff) | 显示在anchor控件的下方 |
showAtLocation(View parent, int gravity, int x, int y) | 显示在parent控件的某个位置 |
一.PopupWindow介绍
PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。
二.如何自定义窗体
(1)构造方法:public PopupWindow (Context context):context上下文对象
(2)必须设置的3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
(3)显示窗体:
a。显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b。指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT
三.实现点击右上角弹出窗体
步骤1.实例化PopupWindow对象
步骤2.设置自定义布局、宽度和高度
步骤3.指定位置显示: showAsDropDown() showAtLocation()
(1)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=".Main4Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_id"
android:text="点我"
android:textSize="30sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
(2)自定义弹出框布局
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出菜单1"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出菜单2"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出菜单3"/>
</LinearLayout>
</LinearLayout>
(3)Java文件
package com.example.day02_homework;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
public class Main4Activity extends AppCompatActivity {
private TextView textId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
textId = (TextView) findViewById(R.id.text_id);
textId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
show_PopupWindow();
}
});
}
private void show_PopupWindow() {
//实例化对象
PopupWindow popupWindow = new PopupWindow(this);
//设置属性
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_right_popupwindow, null);
popupWindow.setContentView(inflate);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//点击外部消失
popupWindow.setOutsideTouchable(true);
//展示
popupWindow.showAsDropDown(textId,0,0);
}
}
四.底部弹出窗体
(1)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=".Main5Activity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="底部弹出"
android:onClick="click"/>
</RelativeLayout>
(2)自定义布局
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="底部弹出1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="底部弹出2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="底部弹出3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="底部弹出4"/>
</LinearLayout>
(3)Java代码
package com.example.day02_homework;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
public class Main5Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
}
public void click(View view) {
show_PopupWindow();
}
private void show_PopupWindow() {
PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_bottom_popupwindow, null);
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
View view = LayoutInflater.from(this).inflate(R.layout.activity_main5, null);
popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
}
}
五.弹出窗体背景半透明
安卓中,控件都有透明度alpha属性,取值范围为0-1的float类型,半透明就是0.5f
在底部弹出的Java代码基础上
package com.example.day02_homework;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
public class Main5Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
}
public void click(View view) {
show_PopupWindow();
}
private void show_PopupWindow() {
PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_bottom_popupwindow, null);
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
//设置当前窗体的属性
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
//设置半透明
layoutParams.alpha=0.5f;
//为窗体设置新属性
getWindow().setAttributes(layoutParams);
//窗体消失恢复透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha=1f;
getWindow().setAttributes(params);
}
});
View view = LayoutInflater.from(this).inflate(R.layout.activity_main5, null);
popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
}
}
六.设置动画
1.在res/anim文件夹下定义进出场动画
duration:动画时长
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="2000" android:fromYDelta="0" android:toYDelta="100%"></translate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="2000" android:fromYDelta="100%" android:toYDelta="0"></translate>
</set>
2.定义进出场动画
<style name="MyPopupWindow">
<item name="android:windowEnterAnimation">@anim/in_duration</item>
<item name="android:windowExitAnimation">@anim/out_duration</item>
</style>
3.为popupwindow设置动画
setAnimationStyle(R.style.MyPopupWindow);