最终要达到的效果图

android-example

步骤:

1.定义一个相应的布局 popupwindow_fontstyle.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=”wrap_content”
    android:orientation=”vertical” android:background=”#ffffff”>

    <ListView android:id=”@+id/text_fontstyle_lv” android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”  />
</LinearLayout>

 

2.程序代码

// 字体
        final TextView view_fontstyle = (TextView) this
                .findViewById(R.id.text_fontstyle);
        if (view_fontstyle != null) {
            view_fontstyle.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    // 处理一个弹出窗口列表

                    // 创建弹出窗口
                    LayoutInflater inflater = (LayoutInflater) getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    final View vPopupWindow = inflater.inflate(
                            R.layout.popupwindow_fontstyle, null, false);

                    final PopupWindow pw = new PopupWindow(vPopupWindow,
                            findViewById(R.id.text_fontstyle).getWidth() – 2,
                            300, true);

                    vPopupWindow.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            pw.dismiss();
                        }

                    });

                    // 初始化list

                    ListView list = (ListView) vPopupWindow
                            .findViewById(R.id.text_fontstyle_lv);

                    final List<Font> fontList = FontManager.getFontList();
                    FontAdapter mFontAdapter = new FontAdapter(arg0
                            .getContext(), fontList);

                    list.setAdapter(mFontAdapter);

                    list.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int location, long arg3) {

                            Font font = fontList.get(location);
                            if (font != null) {
                                view_fontstyle.setText(font.name);
                                //todo 动作

                            }
                            pw.dismiss();

                        }
                    });

                    pw.setOutsideTouchable(true);

                    pw.setFocusable(true);

                    pw.setOnDismissListener(new OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            pw.dismiss();
                        }
                    });
                    TextView mImageView = (TextView) findViewById(R.id.text_fontstyle);
                    // pw.update();
                    pw.showAsDropDown(mImageView, 1,
                            -mImageView.getPaddingBottom());

                }
                           });
        }