package com.example.day2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
public class popwindowActivity extends AppCompatActivity {
private ImageView add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popwindow);
add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建对象
PopupWindow popupWindow = new PopupWindow(popwindowActivity.this);
//加载布局
View inflate = LayoutInflater.from(popwindowActivity.this).inflate(R.layout.myself_popwindow, null);
//设置三要素
popupWindow.setContentView(inflate);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(500);
popupWindow.setAnimationStyle(R.style.pop_window);
//设置点击外部取消
popupWindow.setOutsideTouchable(true);
//设置弹出位置
popupWindow.showAsDropDown(add,0,0);
}
});
}
}
相对性的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".popwindowActivity"
android:orientation="vertical"
>
<RelativeLayout
android:background="#C0CCCC"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:gravity="center_vertical"
android:textColor="#585656"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:text="微信"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/add"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:layout_marginTop="11dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
</LinearLayout>
自定义的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#45494A"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#fff"
android:textSize="20sp"
android:text="发起群聊"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#fff">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#fff"
android:textSize="20sp"
android:gravity="center"
android:text="发起群聊"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#fff">
</View>
</LinearLayout>
进场动画
anim文件夹
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
>
<!--duration持续时间 translate移动方向 alpha透明度的变化-->
<translate android:fromYDelta="-300" android:toYDelta="0"/>
<alpha android:fromAlpha="0.1" android:toAlpha="1"/>
</set>
设置动画效果
<style name="pop_window" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop_in</item>
</style>