2011.07.05(2)——— android PopupWindow

本文详细介绍如何使用 Android 的 PopupWindow 控件实现自定义布局、背景样式及动态展示效果。文章通过实例展示了 PopupWindow 的布局文件配置方法,包括边框圆角背景设置及动画效果实现,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2011.07.05(2)——— android PopupWindow

参考:[url]http://www.eoeandroid.com/thread-48051-1-1.html[/url]
[url]http://disanji.net/2010/12/08/android-popupwindow-2/[/url]
[url]http://www.iteye.com/topic/604462[/url]

1、新写一个layout文件 作为popupwindow的布局文件

<?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"
android:gravity="center"
android:background="#c0000000">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/popupwindow">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dip">
<TextView
android:text="课程:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/popupwindow_class"
android:layout_width="200dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:text="添加"
android:id="@+id/popupwindow_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="取消"
android:id="@+id/prpupwindow_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>


2、布局文件当中 android:background="@drawable/popupwindow" 的边框圆角背景

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#c0000000"
android:endColor="#c0000000"
android:angle="90" /><!--背景颜色渐变 -->
<stroke
android:dashWidth="2dp"
android:dashGap="2dp"
android:width="2dp"
android:color="#FF00ff00"></stroke>
<!--描边 -->
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" /><!--设置圆角-->
</shape>


3、显示popupwindow时的动态效果
res/anim/popuwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:duration="3000" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="3000" />
</set>


4、代码

/**
* 添加课程的popupwindow
* @param v
*/
private void initPopupWindow(View v){
final View view = this.getLayoutInflater().inflate(R.layout.popupwindow, null);
popupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, true);
Button btnOK=(Button)view.findViewById(R.id.popupwindow_add);
btnOK.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
EditText et=(EditText)view.findViewById(R.id.popupwindow_class);
。。。。。。//一些处理
});

//Cancel按钮及其处理事件
Button btnCancel=(Button)view.findViewById(R.id.prpupwindow_cancel);
btnCancel.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
popupWindow.dismiss();//关闭
}
});
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
popupWindow.setAnimationStyle(R.anim.popupwindow);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值