怎样在GridView底部添加按钮

如图,右边是一个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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值