您的位置:首页 > 运维架构

自定义popwindow,从底部弹出和消失动画

2016-07-09 22:10 471 查看
1.使用方法

popWindow = new CustomPopWindow(this, new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
});
popWindow.showAtLocation(this.findViewById(R.id.etActivityEditFeedBack),Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置


2.自定义popwindow

public class CustomPopWindow extends PopupWindow implements View.OnClickListener {

private TextView OK;
private TextView GiveUp;
private TextView Cancel;
private View mMenuView;

public CustomPopWindow(Activity context, View.OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.custom_popwindow, null);
mMenuView.setOnClickListener(this);
OK = (TextView) mMenuView.findViewById(R.id.tvPopwindowOK);
GiveUp = (TextView) mMenuView.findViewById(R.id.tvPopwindowGiveUp);
Cancel = (TextView) mMenuView.findViewById(R.id.tvPopwindowCancel);
Cancel.setOnClickListener(this);
OK.setOnClickListener(itemsOnClick);
GiveUp.setOnClickListener(itemsOnClick);

//设置SelectPicPopupWindow的View
setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
//        //设置SelectPicPopupWindow弹出窗体的高
setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
ColorDrawable dw = new ColorDrawable(0x00000000);
setBackgroundDrawable(dw);
setFocusable(true);
setAnimationStyle(R.style.popwindow_anim_style);
}

@Override
public void onClick(View v) {
dismiss();
}
}


3.在style中设置动画样式

<!--  Popwindow样式 -->
<style name="popwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popwindow_show_anim</item>
<item name="android:windowExitAnimation">@anim/popwindow_hidden_anim</item>
</style>


4.popwindow布局custom_popwindow.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:background="#90000000"
android:gravity="bottom|center_horizontal"
android:orientation="vertical">

<TextView
android:id="@+id/tvPopwindowOK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_titlebar"
android:gravity="center"
android:padding="@dimen/padding_10"
android:text="完成编辑"
android:textSize="@dimen/text_size_18" />

<TextView
android:id="@+id/tvPopwindowGiveUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_titlebar"
android:gravity="center"
android:padding="@dimen/padding_10"
android:text="放弃编辑"
android:textColor="@color/color_red"
android:textSize="@dimen/text_size_18" />

<TextView
android:id="@+id/tvPopwindowCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_10"
android:layout_marginTop="@dimen/padding_10"
android:background="@drawable/background_titlebar"
android:gravity="center"
android:padding="10dp"
android:text="取消"
android:textSize="@dimen/text_size_18" />
</LinearLayout>


5.anim 动画样式

popwindow_show_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/
4000
android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>

popwindow_hidden_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="100%p" />
</set>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: