2012-03-21 67 views
1

我試圖從ListView的setOnItemClickListener顯示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> 

回答

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); 
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 
       } 
相關問題