LinearLayout权重在ScrollView中不起作用?

时间:2015-07-15 08:57:47

标签: android android-layout scrollview android-linearlayout

我在GridView内有com.daimajia.slider.library.SliderLayoutLinearLayout。此LinearLayoutScrollView的孩子。我已为GridViewSliderLayout给予了分量。问题是SliderLayout没有扩展

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:focusableInTouchMode="true"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_main_black"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9.5"
            android:minHeight="335dp" >

            <com.daimajia.slider.library.SliderLayout
                android:id="@+id/advertiseSlider"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <com.daimajia.slider.library.Indicators.PagerIndicator
                android:id="@+id/custom_indicator"
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_gravity="bottom"
                android:gravity="center"
                custom:selected_color="#FFFFFF"
                custom:selected_height="8dp"
                custom:selected_padding_left="3dp"
                custom:selected_padding_right="3dp"
                custom:selected_width="8dp"
                custom:shape="oval"
                custom:unselected_color="#555555"
                custom:unselected_height="4dp"
                custom:unselected_padding_left="3dp"
                custom:unselected_padding_right="3dp"
                custom:unselected_width="4dp" />
        </FrameLayout>

        <ScrollableGridView
            android:id="@+id/categorylist"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="2dp"
            android:layout_weight="5"
            android:background="@color/app_main_black"
            android:drawSelectorOnTop="true"
            android:horizontalSpacing="2dp"
            android:numColumns="2"
            android:stretchMode="columnWidth" />
    </LinearLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:1)

试试这个。它根据孩子计算网格视图高度

public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) {
        ListAdapter listAdapter = gridView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int items = listAdapter.getCount();
        int rows = 0;

        View listItem = listAdapter.getView(0, null, gridView);
        listItem.measure(0, 0);
        totalHeight = listItem.getMeasuredHeight();

        float x = 1;
        if( items > columns ){
            x = items/columns;
            rows = (int) (x + 1);
            totalHeight *= rows;
        }

        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight;
        gridView.setLayoutParams(params);

}

在gridview上调用setAdapter后,只需调用

即可

setGridViewHeightBasedOnChildren( <yourGridView> , <no of grid view columns> )