易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Android中PopupWindow的使用
Android中PopupWindow的使用
public
class
PopUpActivity
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(
this
);
// 引入窗口配置文件
View view = inflater.inflate(R.layout.main2,
null
);
// 创建PopupWindow对象
final
PopupWindow pop =
new
PopupWindow(view, LayoutParams.WRAP_CONTENT, 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.showAsDropDown(v);
}
}
});
}
}
main.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=
"dianji"
/>
</LinearLayout>
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=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"AAAAA"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"BBBBB"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"CCCCC"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"DDDDD"
/>
</LinearLayout>
效果很不错~~~
更多Android相关信息见
Android
专题页面
http://www.linuxidc.com/topicnews.aspx?tid=11
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图