[Android]popupwindow 模拟新浪、腾讯title弹框效果
2013-09-27 16:39
369 查看
public class MainActivity extends Activity { private Button button; private PopupWindow popupWindow; private LinearLayout layout; private ListView listView; private String title[] = { "全部", "我的微博", "周边", "智能排版", "同学" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); button = (Button) findViewById(R.id.tv_title); 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); } }); } 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; } }); } }
dialog.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:background="@drawable/click" android:cacheColorHint="#00000000" android:orientation="vertical" > <ListView android:id="@+id/lv_dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:listSelector="@drawable/grouplist_item_bg_normal" > </ListView> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/background_light" android:orientation="vertical" > <Button android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="title" android:textSize="20sp" /> </LinearLayout>
text.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" > <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="5dp" android:textSize="20sp" /> </LinearLayout>
效果
是下载别人的demo 那个博客找不到了,地址没贴出来,,嘿嘿
相关文章推荐
- Android webservice的用法详细讲解
- Android 保存文件至SharedPreference
- Android平台调用WebService详解
- Mac上Android开发环境的搭建
- Android开发之WebService介绍
- Android调用天气预报的WebService简单例子
- 关于android 中自定义spinner控件
- Android强制设置横屏或竖屏
- Android发送邮件
- Android2.3 statusbar总结
- Android 混淆打包
- 自己封装的Android sqlite-helper.jar包使用方法
- Android SurfaceView绘制
- 【Android】安卓闪电复习
- Android下载文件的进度条提示
- android 简易通讯录查询
- Android FrameWork——StatusBar
- Android中十六进制颜色字符串转int的方法
- TeamViewer,Android摇控PC的东东
- listview android:cacheColorHint,android:listSelector属性作用