ListView的setOnItemClickListener中的PopupWindow未显示

时间:2012-03-21 18:55:23

标签: android popupwindow

我正在尝试从ListView的setOnItemClickListener中显示PopupWindow,但没有显示任何内容。

我做错了什么?

由于

HomeActivity.java

public class HomeActivity extends BaseActivity {

    /* ... */

    PopupWindow popUp;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        /* ... */

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id){                

                LinearLayout layout;
                TextView tv;
                LayoutParams params;

                popUp = new PopupWindow(HomeActivity.this);
                layout = new LinearLayout(HomeActivity.this);
                tv = new TextView(HomeActivity.this);
                params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                  LayoutParams.WRAP_CONTENT);
                layout.setOrientation(LinearLayout.VERTICAL);
                tv.setText("Hi this is a sample text for popup window");
                tv.setTextColor(Color.RED);
                tv.setTextSize(50);
                layout.addView(tv, params);
                popUp.setContentView(layout);
                popUp.showAtLocation(findViewById(R.id.base_layout), Gravity.CENTER, 500, 500);                

            }

        });

        /* ... */

    }
}

BaseActivity.java

public abstract class BaseActivity extends ListActivity{

    private EditText search_field;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.base);
    }
}

base.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/base_layout" >

    <!-- ... -->

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

显示我何时使用

layout = new LinearLayout(HomeActivity.this);
/* ... */
popUp = new PopupWindow(layout,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
popUp.showAtLocation(findViewById(R.id.base), Gravity.CENTER, 500, 500);   

而不是

popUp = new PopupWindow(HomeActivity.this);
layout = new LinearLayout(HomeActivity.this);
/* ... */
popUp.setContentView(layout);
popUp.showAtLocation(findViewById(R.id.base_layout), Gravity.CENTER, 500, 500);

答案 1 :(得分:0)

使用它:

//popAwindow

private void popAwindow(View parent) {    
        if (window == null) {    
            LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
            View v = lay.inflate(R.layout.popupwindow, null);    
            v.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_view));    

            cancel = (Button) v.findViewById(R.id.cancel);    
            cancel.setOnClickListener(cancelListener);    

            window = new PopupWindow(v, 500,260);    
        }    
        window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));      
        window.setFocusable(true);    
        window.update();    
        window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);    
    }    
        OnClickListener cancelListener=new OnClickListener(){    
        @Override    
        public void onClick(View v){    
            closeWindow();    
        }    
    }; 


//for cloasing popupwindow"

    private void closeWindow(){       
        if (window != null) {    
            window.dismiss();    
        }    
    } 


// call on list item click :

         @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id){                
    popAwindow(v);//your popupwindow
                }