简体   繁体   中英

LinearLayout weight is not working inside ScrollView?

I have a GridView and com.daimajia.slider.library.SliderLayout inside a LinearLayout . And this LinearLayout is a child of a ScrollView . I have given weight for GridView and SliderLayout . The problem is that SliderLayout is not expanding

<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>

try this.It calculates grid view height based on 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);

}

After you have called setAdapter on your gridview, just call

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

当您在LinearLayout上声明android:weightSum时,将使用android:layout_weight

android:weightSum = "14.5"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM