View.getWidth随机返回2个不同的值

时间:2012-03-22 12:52:55

标签: android view width

我在ListView的项目附近显示PopupWindow。为此,我在我的listView.setOnItemClickListener()中调用了一个showPopup方法。

我们的想法是使用相同的列表行布局(menu_row.xml)在弹出窗口中显示一些子类别。

我的问题是,有时showPopup(...)中的view.getHeight()会返回一个值(46),有时会返回另一个(76)(使用相同的设备),因此弹出窗口中行的方面会发生变化,而:

1- ListView中的行不会更改

2- view.getHeight()似乎没有改变

有人可以帮忙吗?

listView.setOnItemClickListener(...)

listView.setOnItemClickListener(new OnItemClickListener() {

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

        mAdapter.mSelected = position;
        mAdapter.notifyDataSetChanged();

        if(GlobalVars.menuItemArray[position].children==null){
            // TODO launch activity
        }
        else{

            showPopup(HomeActivity.this, layout_base, v, position);
        }
    }
});

showPopup(...)

public void showPopup(Context context, View parent, View view, int position){

    /*
     * Compute popup position and size
     */

    int[] itemPosition = new int[2];
    view.getLocationOnScreen(itemPosition);

    int item_width = view.getWidth();
    int item_height = view.getHeight();

    int nChildren = GlobalVars.menuItemArray[position].children.size();

    int popup_height = item_height * nChildren; 

    int popup_x = itemPosition[0] + item_width;
    int popup_y = itemPosition[1] + item_height/2 - popup_height/2;

    /*
     * Build the view
     */

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout popupContainer = new LinearLayout(context);
    popupContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    popupContainer.setOrientation(LinearLayout.VERTICAL);

    LayoutParams params;

    params = new LayoutParams(item_width, item_height);
    popupContainer.setOrientation(LinearLayout.VERTICAL); 

    for(int i=0; i<nChildren; i++){

        RelativeLayout menu_row = (RelativeLayout) inflater.inflate(R.layout.menu_row, null);        

        popupContainer.addView(menu_row, params);
    }

    mPopup.setContentView(popupContainer);
    mPopup.setHeight(popup_height);
    mPopup.setWidth(item_width);

    mPopup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, popup_x, popup_y);         

}

menu_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_row_container"
    android:layout_width="250dp"
    android:layout_height="46dp"
    android:background="@drawable/bg_menu_cell_inactive"
    android:paddingRight="4dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="20dp"
        android:singleLine="true"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/num_items"
        android:layout_width="40dp"
        android:layout_height="23dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/bg_badge_cell"
        android:gravity="center"
        android:textColor="#ffffff" />

</RelativeLayout>

0 个答案:

没有答案