如图,右边是一个GridView,底部有两个按钮。当Gridview内容比较少的时候,我们之间用个线性布局就可以达到这种效果。
但是,当我们内容很多的时候,超出一个页面时,linearlayout就只能显示GridView的数据的,底部的两个按钮会因为GridView已经充满屏幕而不显示。
解决方法:
把GridView设置为不可滑动,这个要自定义一个类来继承GridView。测量它的高度,把高度设为尽可能大。、
代码如下:
public class NoScrollGridView extends GridView { public NoScrollGridView(Context context) { super(context); } public NoScrollGridView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
然后把GridView和两个按钮写在同一个线性布局里面。再在外面嵌套一个ScrollView。就达到想要的效果了
<ScrollView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.78" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.wanwu.power.adapter.NoScrollGridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="6dp" android:numColumns="2" android:scrollbars="none"></com.wanwu.power.adapter.NoScrollGridView> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="22dp" android:layout_marginRight="22dp" android:layout_marginTop="36dp"> <TextView android:id="@+id/btn_all_open" android:layout_width="0dp" android:layout_height="36dp" android:layout_marginLeft="22dp" android:layout_marginRight="22dp" android:layout_weight="1" android:background="@drawable/btn_bg_blue_15" android:gravity="center" android:text="全部开启" android:textColor="#ffffff" android:textSize="14sp" /> <TextView android:id="@+id/btn_all_close" android:layout_width="0dp" android:layout_height="36dp" android:layout_marginLeft="22dp" android:layout_marginRight="22dp" android:layout_weight="1" android:background="@drawable/btn_bg_red_15" android:gravity="center" android:text="全部关闭" android:textColor="#ffffff" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="invisible" /> </LinearLayout> </LinearLayout> </ScrollView>