遇到一个问题,先记录一下
PopWindow自定义动画
import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Build; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; public class ActivityMain extends Activity { private int screenWidth; private int screenHeigh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("xirtam " + Build.VERSION.SDK); System.out.println("xirtam " + Build.VERSION.SDK_INT); {// 获取屏幕宽高 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeigh = dm.heightPixels; } LayoutInflater inflater = LayoutInflater.from(this); // 引入窗口配置文件 View view = inflater.inflate(R.layout.main2, null); // 创建PopupWindow对象 final PopupWindow pop = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, false); Button btn = (Button) findViewById(R.id.btn); // 需要设置一下此参数,点击外边可消失 pop.setBackgroundDrawable(new BitmapDrawable()); // 设置点击窗口外边窗口消失 pop.setOutsideTouchable(true); // 设置此参数获得焦点,否则无法点击 pop.setFocusable(true); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (pop.isShowing()) { // 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏 pop.dismiss(); } else { // 显示窗口 pop.setAnimationStyle(R.style.PopupAnimation); pop.showAtLocation(v, Gravity.NO_GRAVITY, 0, screenHeigh); // pop.showAsDropDown(v); pop.update(); } } }); } }
layout/main1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我是小狗" /> </LinearLayout>
layout/main2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/a" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="小狗你好" /> </LinearLayout>
values/styles.xml
<style name="PopupAnimation" mce_bogus="1" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/enter</item> <item name="android:windowExitAnimation">@anim/exit</item> </style>
anim/enter.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3500" android:fromYDelta="100%" android:toYDelta="1%" > </translate>
anim/exit.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3500" android:fromYDelta="1%" android:toYDelta="100%" > </translate>
问题是在nexus5上 showAtLocation 没反应,其他机型未发现此问题。