Android之ScrollView设置maxHeight和图片上下滑动简单实现

实现步骤:

1.说道这个问题,很多人说控件不就有这个属性吗?不是挺简单,你能看到这文章说明你还是试了不行才百度的,在xml里设置控件属性没用,类似的控件还有listview等滑动控件,都是没有这个属性的。最近项目遇到类似问题,中间的优惠券其实是一张图片,并不是数据,需求说:图片小于三个item的时候不滑动且自适应,大于三个item的时候imagview可上下滑动,功能很简单,在imagview控件外层套一个scrollview,图片控件铺满自适应,scrollview自适应设置一个最大高度就OK了,为了设置最大值所以我们的自定义scrollview、
2.自定义的scrollview、

package com.zjtd.bzcommunity.util;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.ScrollView;

/**
 * Created by wergr on 2018/5/17.
 * 自定义scrollview
 */

public class ScrollViewYhq extends ScrollView {
    private Context mContext;


    public ScrollViewYhq(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public ScrollViewYhq(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            //最大高度显示为屏幕内容高度的一半
            Display display = ((Activity) mContext).getWindowManager().getDefaultDisplay();
            DisplayMetrics d = new DisplayMetrics();
            display.getMetrics(d);
            //此处是关键,设置控件高度不能超过屏幕高度一半(d.heightPixels / 2)
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(700, MeasureSpec.AT_MOST);//这里我设置了700

        } catch (Exception e) {
            e.printStackTrace();
        }
        //重新计算控件高、宽
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

2.在xml中引用这个自定义scrollview、

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layou

t_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/store_bgimgyhq"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imagtop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@mipmap/index_tanimg1" />

    <com.zjtd.bzcommunity.util.ScrollViewYhq
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <ImageView
            android:id="@+id/imagyhq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"

            android:src="@mipmap/index_tanimg2" />
    </com.zjtd.bzcommunity.util.ScrollViewYhq>

    <ImageView
        android:id="@+id/imagbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@mipmap/index_tanimg3" />

    <ImageView
        android:id="@+id/imagcha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:scaleType="fitXY"
        android:src="@mipmap/index_tanchuan_xx" />
</LinearLayout>

----------就是这么简单,欢迎指教、

作者:烟味少男
来源:CSDN
原文:https://blog.csdn.net/android_cll/article/details/80350090
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值