展会信息港展会大全

android popupwindow 模拟新浪、腾讯title选项卡
来源:互联网   发布日期:2015-10-03 13:17:37   浏览:1788次  

导读:阀先在上节中是使用dialog 实现的,(点击连接),现在我就讲些popupwindow 的实现,这个相对dialog比较简单,因为不用自定义dialog. 实现代码很简单如下:...

首先在上节中是使用dialog 实现的,(点击连接),现在我就讲些popupwindow 的实现,这个相对dialog比较简单,因为不用自定义dialog.

实现代码很简单如下:

代码片段:

[java]

public void showPopupWindow(int x, int y) {

layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(

R.layout.dialog, null);

listView = (ListView) layout.findViewById(R.id.lv_dialog);

listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,

R.layout.text, R.id.tv_text, title));

popupWindow = new PopupWindow(MainActivity.this);

popupWindow.setBackgroundDrawable(new BitmapDrawable());

popupWindow

.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2);

popupWindow.setHeight(300);

popupWindow.setOutsideTouchable(true);

popupWindow.setFocusable(true);

popupWindow.setContentView(layout);

// showAsDropDown会把里面的view作为参照物,所以要那满屏幕parent

// popupWindow.showAsDropDown(findViewById(R.id.tv_title), x, 10);

popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT

| Gravity.TOP, x, y);//需要指定Gravity,默认情况是center.

listView.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

button.setText(title[arg2]);

popupWindow.dismiss();

popupWindow = null;

}

});

}

[java]

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

button.getTop();

int y = button.getBottom() * 3 / 2;

int x = getWindowManager().getDefaultDisplay().getWidth() / 4;

showPopupWindow(x, y);

}www.2cto.com

});

样子我就不贴了,和前面一章dialog显示的一样.

在这里遇到个小问题:int y = button.getBottom() * 3 / 2;这里获取的y坐标应该是 button.getBottom();可是这样写popupwindow就显示位置不对了,在button中间,不知道为什么。

如果知道为什么了通知我一下,共同学习嘛,先谢谢了。

作者:jj120522

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港