2014-05-02 40 views
1

我想實現一個彈出窗口與列表視圖。當我在listview中添加項目時,它將填充錨點視圖下方的屏幕。彈出窗口與列表視圖不包裝

基本上我的列表視圖不包裝。 以下是圖像和代碼,我使用: -

button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //    ToolTipView toolTipView = new ToolTipView(MainActivity.this, ""); 
      //    toolTipView.setText("swfwefw wefwe8732ryf89 8yu2fc h8wuijcb8unwi "); 
      //    toolTipView.show(v); 
      LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.popupwindoiw, null); 

      final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

      ListView btnDismiss = (ListView)popupView.findViewById(R.id.list); 
      final ArrayList<String> list = new ArrayList<String>(); 
      list.add("hello"); 
      list.add("helloworld"); 
      list.add("helloexzeo"); 


      ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_row, list); 
      btnDismiss.setAdapter(adapter); 
      btnDismiss.setOnItemClickListener(new OnItemClickListener() { 

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

        Toast.makeText(MainActivity.this, list.get(arg2), Toast.LENGTH_SHORT).show(); 
        popupWindow.dismiss(); 
       } 
      }); 

      popupWindow.showAsDropDown(v); 

     } 
    }); 

popwindow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </ListView> 

</RelativeLayout> 

list_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" /> 

在圖像中,目前的ListView在紅色區域顯示,但我想在藍色區域。

,因爲我支持多種屏幕尺寸 enter image description here

+0

儘量給你popwindow佈局固定寬度。 – GrIsHu

+0

Popupwindow位置可以在任何地方也..我已經提到。不能給寬度。必須包裝 –

回答

1

可以測量適配器含量最大視圖的寬度,並啓動popwindow這個寬度,我不能使用自定義的寬度和高度的popupwindow的。

你可以使用這個方法來衡量在適配器含量最大的視圖 - }

private int measureContentWidth(ListAdapter listAdapter) { 
ViewGroup mMeasureParent = null; 
int maxWidth = 0; 
View itemView = null; 
int itemType = 0; 

final ListAdapter adapter = listAdapter; 
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
final int count = adapter.getCount(); 
for (int i = 0; i < count; i++) { 
    final int positionType = adapter.getItemViewType(i); 
    if (positionType != itemType) { 
     itemType = positionType; 
     itemView = null; 
    } 

    if (mMeasureParent == null) { 
     mMeasureParent = new FrameLayout(mContext); 
    } 

    itemView = adapter.getView(i, itemView, mMeasureParent); 
    itemView.measure(widthMeasureSpec, heightMeasureSpec); 

    final int itemWidth = itemView.getMeasuredWidth(); 

    if (itemWidth > maxWidth) { 
     maxWidth = itemWidth; 
    } 
} 

return maxWidth; 

可以啓動popupwindow這樣的 -

PopupWindow popupWindow = new PopupWindow(popupView,measureContentWidth(adapter), LayoutParams.WRAP_CONTENT);