• ViewPager中height=wrap_content无效,ScrollView里边用ListView显示不全解决办法


    ViewPager中height=wrap_content无效

    public class MyViewPager extends ViewPager {
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int childCount = getChildCount();
            int height = 0;
            int expandSpec = 0;
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                ViewGroup.LayoutParams lp = child.getLayoutParams();
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
                child.measure(widthMeasureSpec, getChildMeasureSpec(heightMeasureSpec, 0, lp.height));
                int childHeight = child.getMeasuredHeight();
                if (childHeight > height){
                    height = childHeight;
                }
    
                expandSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            }
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
    
        public MyViewPager(@NonNull Context context) {
            super(context);
        }
    
        public MyViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    }
    ScrollView里边用ListView显示不全
    public class MyListView extends ListView {
        public MyListView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        /**
         * 自己测量解决显示不全的问题
         * @param widthMeasureSpec
         * @param heightMeasureSpec
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
    }
    inflate(R.layout.activity_main, parent, false)解决设置无效问题,包一层不行

    setContentView(R.layout.activity_main);

    AppCompatActivity###
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
    getDelegate().setContentView(layoutResID);
    }

    abstract class AppCompatDelegate的实现类AppCompatDelegateImpl###
    @Override
    public void setContentView(int resId) {
    ensureSubDecor();
    ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content);
    contentParent.removeAllViews();
    LayoutInflater.from(mContext).inflate(resId, contentParent); /// 最终调用inflate
    mAppCompatWindowCallback.getWrapped().onContentChanged();
    }
    
    
    abstract class LayoutInflater###
    public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    ……………………………………………………………………………………
    final View temp = createViewFromTag(root, name, inflaterContext, attrs); //1
    ……………………………………………………………………………………
    temp.setLayoutParams(params); //2
    ……………………………………………………………………
    rInflateChildren(parser, temp, attrs, true);
    ………………………………………………………………………………
    root.addView(temp, params);
    ………………………………………………………………………………
    if (root == null || !attachToRoot) {
    result = temp;
    }
    return result;
    }
    }






  • 相关阅读:
    微信小程序 结合公众号前后端全栈开发微信优惠卡券
    微信跳转的一些区别,markdown备用
    微信小程序真机调试中一些小问题
    使用mpvue实现动态图片波浪图效果
    今天准备开通博客。记录第一天
    .NetCore打包nuget包含依赖
    kubernetes-dashboard 2.x 版本安装
    删除kubernetes dashboard
    Centos 8 kubernetes 安装笔记
    ABP使用NSwagStudio for Swagger Api生成ServiceProxies
  • 原文地址:https://www.cnblogs.com/anny0920/p/12757670.html
Copyright © 2020-2023  润新知