您的位置:首页 > 移动开发 > Android开发

[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 那个博客找不到了,地址没贴出来,,嘿嘿
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: