點擊按鈕後彈出一個PopWindow,首先看下效果吧
會遇到的一些問題:
1.關於窗體會被軟件盤遮擋
// 設置pop被鍵盤頂上去,而不是遮擋
popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
第一步給PopWindow 繪出一個佈局
pop_menu_layout.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="wrap_content"
android:orientation="vertical"
android:background="#ffffff" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ok"
android:clickable="true"
android:gravity="center"
android:textColor="@android:color/holo_orange_dark"
android:text="確定" />
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="@+id/cancel"
android:clickable="true"
android:gravity="center"
android:text="取消" />
</LinearLayout>
MainActivity中:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private PopupWindow mPopWindow;
private TextView tv1,tv2;
View contextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopWindow();
initView();
}
private void initPopWindow() {
//設置contentView
contextView = getLayoutInflater().inflate(R.layout.pop_menu_layout, null);
mPopWindow=new PopupWindow(contextView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//設置popWindow出現或消失的動畫(縮放消失,從無到有顯示)
mPopWindow.setAnimationStyle(R.style.popwindow_anim_style);
//設置popWindow外面區域時是否可以消失
mPopWindow.setOutsideTouchable(true);
//設置背景
//mPopWindow.setBackgroundDrawable();
//設置觸摸時是否會有響應
//mPopWindow.setTouchable(true);
}
private void initView() {
tv1= (TextView) contextView.findViewById(R.id.ok);
tv2= (TextView) contextView.findViewById(R.id.cancel);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
}
//Button點擊事件。
public void btnClick(View view){
if(mPopWindow.isShowing())
mPopWindow.dismiss();
else
mPopWindow.showAtLocation(contextView, Gravity.CENTER,0,0);
}
//popWindow 點擊事件
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.ok:
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
mPopWindow.dismiss();
break;
case R.id.cancel:
Toast.makeText(MainActivity.this, "cancel", Toast.LENGTH_SHORT).show();
mPopWindow.dismiss();
break;
}
}
//回收
@Override
protected void onDestroy() {
super.onDestroy();
if(mPopWindow!=null){
mPopWindow.dismiss();
mPopWindow=null;
}
}
}
爲popWindow設置出現消失的動畫。
1.顯示動畫
scale_show.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"/>
</set>
2.消失動畫
scale_dissmiss.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"/>
</set>
在style中寫樣式
<style name="popwindow_anim_style">
<!-- 指定顯示時的動畫xml -->
<item name="android:windowEnterAnimation">@anim/scale_show</item>
<!-- 指定消失時的動畫xml -->
<item name="android:windowExitAnimation">@anim/scale</item>
</style