PopupWindow弹窗
Step 1 创建popup_layout.xml 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/choose"
android:layout_width="60dp"
android:layout_height="30dp"
android:text="选择"
android:textColor="#ffffff"
android:gravity="center"
android:background="#000000"
/>
<TextView
android:id="@+id/copy"
android:layout_width="60dp"
android:layout_height="30dp"
android:text="复制"
android:textColor="#ffffff"
android:layout_marginLeft="1dp"
android:gravity="center"
android:background="#000000"
/>
<TextView
android:id="@+id/paste"
android:layout_width="60dp"
android:layout_height="30dp"
android:text="粘贴"
android:layout_marginLeft="1dp"
android:textColor="#ffffff"
android:gravity="center"
android:background="#000000"
/>
</LinearLayout>
设计样式
Step 2 实例化PopupWindow对象
准备工作:获取被锚定的view 实例 以及点击事件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
TextView popup_window = findViewById(R.id.popup_window);
popup_window.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
...
case R.id.popup_window:
showPopupWindow(findViewById(R.id.popup_window));
break;
default:
break;
}
}
实例化PopupWindow对象——showPopupWindow()方法中
//准备弹出所需要的视图对象,PopupWindow中的第一个参数
View v = LayoutInflater.from(this).inflate(R.layout.popup_layout,null);
//1.实例化对象
//参数1:用在弹窗中的View;参数2:宽;参数3:高;参数4:focusable能否获取焦点
final PopupWindow window = new PopupWindow(v,600,100,true);
Step 3 设置 背景 动画 点击事件
设置背景
//设置背景(透明色)
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//设置能响应外部的点击事件
window.setOutsideTouchable(true);
//设置自己能响应点击事件
window.setTouchable(true);
设置动画
四种动画类别
translate 移动动画
rotate 旋转动画
scale 缩放动画
alpha 透明动画
①创建动画资源
res→New→ Android Source Directory
(Directory name : anim Source type:anim)
translate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="200"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="1000"
></translate>
</set>
scale.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:pivotX="300"
android:pivotY="100"
android:duration="1000"
></scale>
</set>
②创建style 来 应用动画资源
<style name="translate_anim" >
<item name="android:windowEnterAnimation" >@anim/translate</item>
</style>
<style name="scale_anim" >
<item name="android:windowEnterAnimation" >@anim/scale</item>
</style>
③对当前弹窗的动画风格设置为已创建的style索引
window.setAnimationStyle(R.style.scale_anim);
// window.setAnimationStyle(R.style.translate_anim);
设置点击事件
//为弹窗中的文本添加点击事件
TextView choose = v.findViewById(R.id.choose);
TextView copy = v.findViewById(R.id.copy);
TextView paste = v.findViewById(R.id.paste);
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"点击了选择",Toast.LENGTH_SHORT).show();
//点击完以后 弹窗消失
window.dismiss();
}
});
copy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"点击了复制",Toast.LENGTH_SHORT).show();
//点击完以后 弹窗消失
window.dismiss();
}
});
paste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
//点击完以后 弹窗消失
window.dismiss();
}
});
强调:一定在 设计完点击事件后 记得 调用 window.dismiss(); 让弹窗消失
Step 4 显示
//3.显示
//参数1(anchor锚):以锚作为参照物,在下方展示
//参数2,3:相对于锚在x,y方向上的偏移
window.showAsDropDown(view,-300,-200);
测试结果图
缩放动画
移动动画