ListVIew项目布局高度取决于项目数量

时间:2015-02-12 17:27:31

标签: android listview

我有ListView的项目,我想根据项目的数量设置每个项目的高度,以便listview填满屏幕。

首先我得到没有条形的窗口大小:

//returns height of the app window in pixels
public static int windowHeight(Context context){

        int barH = 0; //bar height
        int height = 125;//DpToPx(context, 125); //default
        int displayH;

    try {
        //getting stastus bar height
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            barH = context.getResources().getDimensionPixelSize(resourceId);
        }

        //getting all the screen height

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(size);
            displayH = size.y;

        } else {
            //noinspection deprecation
            displayH = display.getHeight();
        }

        height = displayH - barH;
    }
    catch (Exception e){
        e.printStackTrace();
    }
        return height;
    }

然后在MenuAdapter中我试图设置高度:

//set up the height of listrow depending on the amount of menu items in order the listview to fullfill the screen
private void setupRowHeight(LinearLayout l) {
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)l.getLayoutParams();
    int margin = lp.topMargin + lp.bottomMargin;
    int itemsAmount = content.size();
    lp.height = Util.windowHeight(context) / itemsAmount - margin*itemsAmount;
    Log.i("height of row", String.valueOf(l.getLayoutParams().height) + " " + String.valueOf(margin));


}

}

RowLayout.xml中的LinearLayout是:

<LinearLayout
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:id="@+id/elem"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/row_height"
    android:background="@drawable/layout_bord"
    android:layout_marginLeft="@dimen/row_margin_horiz"
    android:layout_marginRight="@dimen/row_margin_horiz"
    android:layout_marginTop="@dimen/row_margin_vert"
    android:layout_marginBottom="@dimen/row_margin_vert">
    .....................................................
    </LinearLayout>

但是这些物品的高度并不高。 我只是没有得到我没有考虑到的东西?

0 个答案:

没有答案