PopUpWindow(提高篇)
上一篇只是实现了一个最简单的PopUpWindow显示,本篇介绍显示带列表选择的PopUpWindow和带动画效果的PopUpWindow。
先看效果:
接下来看代码实现。
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout ll_main;
PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll_main = findViewById(R.id.ll_main);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_open_simple_popupwindow:
openSimplePopUpWindow();
break;
case R.id.btn_open_list_popupwindow:
openListPopUpWindow();
break;
case R.id.btn_open_bottom_animation_list_popupwindow:
openBottomAnimationListPopUpWindow();
break;
case R.id.btn_open_left_animation_list_popupwindow:
openLeftAnimationListPopUpWindow();
break;
}
}
private void openSimplePopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_simple_popupwindow, null);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
}
private void openListPopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
ListView listView = view.findViewById(R.id.listview);
final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
dismissPopupWindow();
}
});
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
}
private void openBottomAnimationListPopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
ListView listView = view.findViewById(R.id.listview);
final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
dismissPopupWindow();
}
});
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setAnimationStyle(R.style.bottom_animation);
popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
}
private void openLeftAnimationListPopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
ListView listView = view.findViewById(R.id.listview);
final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
dismissPopupWindow();
}
});
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setAnimationStyle(R.style.left_animation);
popupWindow.showAtLocation(ll_main, Gravity.LEFT, 0, 0);
}
private void dismissPopupWindow() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
}
}
activity_main.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"
android:orientation="vertical"
android:id="@+id/ll_main"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_open_simple_popupwindow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开一个最简单PopUpWindow!"
android:onClick="onClick"
/>
<Button
android:id="@+id/btn_open_list_popupwindow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开一个选择列表PopUpWindow!"
android:onClick="onClick"
/>
<Button
android:id="@+id/btn_open_bottom_animation_list_popupwindow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开一个带动画效果选择列表PopUpWindow!(底部弹出)"
android:onClick="onClick"
/>
<Button
android:id="@+id/btn_open_left_animation_list_popupwindow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开一个带动画效果选择列表PopUpWindow!(左侧弹出)"
android:onClick="onClick"
/>
</LinearLayout>
PopUpWindow用的到布局layout_list_popupwindow.xml
<?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">
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="选择填报学校"
android:textColor="@color/colorAccent"
android:textSize="20sp"/>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="@color/gray"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="@color/gray"/>
</LinearLayout>
打开一个带列表选择的PopUpWindow。
private void openListPopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
ListView listView = view.findViewById(R.id.listview);
final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
dismissPopupWindow();
}
});
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
}
打开一个带列表选择的并且带动画效果的PopUpWindow,打开时是从底部慢慢移入,关闭是从底部慢慢移出。
private void openBottomAnimationListPopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
ListView listView = view.findViewById(R.id.listview);
final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
dismissPopupWindow();
}
});
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
//实现动画效果
popupWindow.setAnimationStyle(R.style.bottom_animation);
popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
}
打开一个带列表选择的并且带动画效果的PopUpWindow,打开是从左侧慢慢移入,关闭是从左侧慢慢移出。
private void openLeftAnimationListPopUpWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
ListView listView = view.findViewById(R.id.listview);
final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
dismissPopupWindow();
}
});
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
//实现动画效果
popupWindow.setAnimationStyle(R.style.left_animation);
popupWindow.showAtLocation(ll_main, Gravity.LEFT, 0, 0);
}
要想显示一个带动画效果的PopUpWindow,只需调用setAnimationStyle,传入一个动画样式资源ID。
本篇用的到的动画效果介绍
底部移入动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.5"
android:toAlpha="1.0" />
</set>
底部移出动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="100%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.5" />
</set>
左侧移入动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="-100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.5"
android:toAlpha="1.0" />
</set>
左侧移出动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0.0"
android:toXDelta="-100%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.5" />
</set>
用到的动画样式
<style name="bottom_animation">
<item name="android:windowEnterAnimation">@anim/enter_bottom</item>
<item name="android:windowExitAnimation">@anim/exit_bottom</item>
</style>
<style name="left_animation">
<item name="android:windowEnterAnimation">@anim/enter_left</item>
<item name="android:windowExitAnimation">@anim/exit_left</item>
</style>
到此PopUpWindow的使用就介绍完了,大家可以任意发挥,实现自己想要的效果。