LinearLayout 权重在 ScrollView 中不起作用?

LinearLayout weight is not working inside ScrollView?

我在 LinearLayout 中有一个 GridViewcom.daimajia.slider.library.SliderLayout。而这个LinearLayoutScrollView的child。我给 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>

android:layout_weight 当您在 LinearLayout

上声明了 android:weightSum 时使用
android:weightSum = "14.5"

尝试this.It根据children

计算网格视图高度
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> )