当前位置: 首页 > news >正文

做易购网站wordpress移动底部菜单

做易购网站,wordpress移动底部菜单,设备报价单模板,自己制作软件平台效果 child按行显示#xff0c;显示不下就换行。 分析 继承ViewGrouponDraw()不重写#xff0c;使用ViewGroup的测量-重点 #xff08;测量child、测量自己#xff09;布局-重点 #xff08;布局child#xff09; 知识点 执行顺序 构造函数 - onMeasure() - …效果 child按行显示显示不下就换行。 分析 继承ViewGrouponDraw()不重写使用ViewGroup的测量-重点 测量child、测量自己布局-重点 布局child 知识点 执行顺序 构造函数 - onMeasure() - onLayout() - onDraw() 问onMeasure()后getMeasuredHeight()和getMeasuredWidth()可以获得到值。 答因为onMeasure()中会使用setMeasuredDimension(width,height)设置宽高值。 问onLayout() 后getHeight()和getWidth()可以获得到值 答getWidth()是使用onLayout() 后的left、right得到的没有布局的时候是没有left、right值的getHeight()同理。 onMeasure()度量 MeasurSpec类 32位整型数据高两位是模式低30位是宽/高数据用位运算来获取数据。一个数据分为两部分 模式 EXACTLY确定的值、AT_MOST需要多少给多少最多不能超过低30位的数据、UNSPECIFIED不指定滚动的时候 用于在向下执行测量的时候传递View能用的宽/高数据。 MeasurSpec值是根据父容器和自己LayoutParam分配给View的大小View需要再onMeasure()时确定自己需要的大小允许你自己调整然后设置。 如何获取MeasurSpec值LayoutParam转化为px View的层级是树形的度量是递归实现的child的度量是由father分发的想要知道MeasurSpec值是如何获取的可以看ViewGroup中的度量child时是如何获得child的MeasurSpec值的。 ViewGroup类中提供了getChildMeasureSpec方法用于获得child的MeasurSpec。 方法参数说明如下 spec 父布局的MeasurSpec padding父布局的padding如获取child宽度的MeasurSpec就需要paddingLeftpaddingRight。 childDimensionchild的布局有xxdp/wrap_content/match_parent三种情况如获取child宽度的MeasurSpec值就是child布局中的android:layout_width值。 经过对getChildMeasureSpec()源码的分析得出如下规则宽高都使用此规则 View的onMeasure() 1. 据MeasureSpec的不同模式确定自己的宽高 如果是MeasureSpec.EXACTLY 使用MeasureSpec中的size 如果是MeasureSpec.AT_MOST使用自身需要的大小 如果是MeasureSpec.UNSPECIFIED0/自身需要的大小 2. 设置View的宽高setMeasuredDimension(width,height) ViewGroup的onMeasure() 除了 View的onMeasure()中要做的还要度量child。 先度量谁自己/child? 一般都是先度量child在度量自己ViewPager除外ViewPager高度设置为wrap_content无效。 流式布局 度量child 度量自己 如果是MeasureSpec.EXACTLY 使用MeasureSpec中的size不用计算。 如果是MeasureSpec.AT_MOST使用自身需要的大小child撑起的宽高。 如果是MeasureSpec.UNSPECIFIED0/自身需要的大小child撑起的宽高。 根据上面规则可知要计算出child撑起的宽高。 child撑起的宽度width 单行总宽度最大值包含水平间隔 paddingLeft paddingRight child撑起的高度height 每行中高度最高的child的高度 垂直间隔 paddingTop paddingBottom 设置初始值 int height getPaddingTop()getPaddingBottom(); int width getPaddingLeft()getPaddingRight(); 循环child记录单行的数据 int lineHeight 0; //记录每行的高度 放入一个child的时候对比取最大值 int curWidth getPaddingLeft()getPaddingRight(); //记录每行的当前宽度 放入一个child的时候加上child的宽度以及水平间隔 换行的时候保存单行数据至width和height并清空单行数据注意换行的判断条件。 注意最后一行执行到最后一个元素时保存最后一行的数据。 另外为了方便布局记录每行的View以及每行的高度布局时不需要再次计算。 另设置自己的宽高的时候记得用规则判断下。 具体实现见文章底部源码。 布局 根据测量的宽高确定child的位置left、right、top、bottom 在自定义ViewGroup中布局child的时候使用的是视图坐标系。 在布局时获取child的宽高使用getMeasuredWidth()/getMeasuredHeight()此时getWidth()/getHeight()还没有数值。 自定义ViewGroup流程 扩展 ViewPager高度设置为wrap_content无效的原因 在度量时没有先度量child先设置了自己的宽高宽高获取使用的是getDefaultSize()方法。 根据文中的获取child的MeasureSpec的规则可知当LayoutParam的值为wrap_content时其MeasureSpec的mode为AI_MOST。 看上面源码可知当modelMeasureSpec.AT_MOST时没有使用根据child计算高度用的是父布局能给的所有高度所以当高度设置为wrap_content高度仍是占满了父布局的全部。 资料 MeasureSpec.UNSPECIFIED说明https://www.cnblogs.com/liushilin/p/11055741.htmlgoogle官方提供的流式布局 flexbox-layout 看的时候注重两点如何测量子布局child.measure和 设置自己宽高的位置setMeasuredDimension github地址https://github.com/google/flexbox-layout 使用文档FlexboxLayout全攻略(Google官方灵活实现流式布局控件)-CSDN博客 注意在真实项目中可以使用FlexboxLayout实现流式布局也可使用RecyclerViewFlexboxLayoutManager实现。 可以自适应child高度的ViewPager自定义ViewPager具体实现见源码 源码 FlowLayout.java package com.learn.customui.custom;import android.content.Context; import android.content.Intent; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup;import java.util.ArrayList; import java.util.List;/*** 问题* 1.没有考虑child的margin* 2.宽度多了一个水平间隔* 3.未处理child的visiable属性* 4.没有考虑gravity*/ public class FlowLayout extends ViewGroup {private int mHorizontalSpace 10; //px 水平间隔private int mVerticalSpace 10; //px 垂直间隔private ListListView mAllViews new ArrayList();//按行存储View用于布局使用private ListInteger lineHeights new ArrayList(); //存储每行的高度public FlowLayout(Context context) {super(context);}public FlowLayout(Context context, AttributeSet attrs) {super(context, attrs);}public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}/**** param widthMeasureSpec 根据该View父布局的宽度MeasureSpec与自身的LayoutParamandroid:layout_width结合获得的MeasureSpec类型值* param heightMeasureSpec 根据该View父布局的高度MeasureSpec与自身的LayoutParamandroid:layout_height结合获得的MeasureSpec类型值**/Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);//会多次测量使用最后一次测量mAllViews.clear();lineHeights.clear();/*** 1 度量Child* 1.1 度量child* 1.2 度量自己的时候需要child的宽高* 重点使用getChildMeasureSpec()获取child的MeasureSpec 规则就是固定的至于能不能显示开发者自定*/for(int i 0;igetChildCount();i){View child getChildAt(i);//获取child宽度的MeasureSpecint childWidthMeasureSpec getChildMeasureSpec(widthMeasureSpec,getPaddingLeft()getPaddingRight(),child.getLayoutParams().width);//获取child高度的MeasureSpecint childHeightMeasureSpec getChildMeasureSpec(heightMeasureSpec,getPaddingTop()getPaddingBottom(),child.getLayoutParams().height);//测量childchild.measure(childWidthMeasureSpec,childHeightMeasureSpec);}//child被度量过可以获取宽高/*** 2 度量自己 根据不同模式下设置不同的大小* MeasureSpec.EXACTLY 使用方法参数中的size* MeasureSpec.AT_MOST 使用子布局撑起来的size* MeasureSpec.UNSPECIFIED 使用方法参数中的size 0/size 0代表不指定滚动时*///child撑起的高度 每行中高度最高的child的高度 垂直间隔 paddingTop paddingBottomint height getPaddingTop()getPaddingBottom();//child撑起的宽度 单行总宽度最大值包含水平间隔 paddingLeft paddingRightint width getPaddingLeft()getPaddingRight();//当前View能用的最大宽度父亲给的int widthSpecSize MeasureSpec.getSize(widthMeasureSpec);//记录每行的高度int lineHeight 0;//记录每行的当前宽度int curWidth getPaddingLeft()getPaddingRight();//记录每行的child 布局时使用ListView lineView new ArrayList();//遍历child 计算高度和宽度for(int i 0;igetChildCount();i){View child getChildAt(i);//换行if(curWidthchild.getMeasuredWidth()widthSpecSize){//存储当前行的数据mAllViews.add(lineView);lineView new ArrayList();lineHeights.add(lineHeight);height lineHeight mVerticalSpace;width Math.max(width,curWidth);//清空lineHeight 0;curWidth getPaddingLeft()getPaddingRight();}//放入childlineHeight Math.max(lineHeight,child.getMeasuredHeight());curWidth child.getMeasuredWidth()mHorizontalSpace;lineView.add(child);//最后一个child 存储当前行数据if(i getChildCount()-1){//最后一行不加垂直间隔height lineHeight;width Math.max(width,curWidth);mAllViews.add(lineView);lineHeights.add(lineHeight);}}if(MeasureSpec.getMode(widthMeasureSpec) MeasureSpec.EXACTLY) width MeasureSpec.getSize(widthMeasureSpec);if(MeasureSpec.getMode(heightMeasureSpec) MeasureSpec.EXACTLY) height MeasureSpec.getSize(heightMeasureSpec);//设置自己的宽高setMeasuredDimension(width,height);}Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int curLeft getPaddingLeft();int curTop getPaddingTop();for (int i 0; i mAllViews.size(); i) {for (View child : mAllViews.get(i)) {child.layout(curLeft,curTop,curLeftchild.getMeasuredWidth(),curTopchild.getMeasuredHeight());curLeft child.getMeasuredWidth()mHorizontalSpace;}curLeft getPaddingLeft();curTop lineHeights.get(i)mVerticalSpace;}} }activity_flow.xml ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticaltools:context.FlowActivitycom.learn.customui.custom.FlowLayoutandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgroundcolor/blackTextViewandroid:layout_width100dpandroid:layout_height50dpandroid:gravitycenterandroid:backgroundcolor/redandroid:text1111/TextViewandroid:layout_width300dpandroid:layout_height50dpandroid:gravitycenterandroid:backgroundcolor/redandroid:text2222/TextViewandroid:layout_width100dpandroid:layout_height50dpandroid:gravitycenterandroid:backgroundcolor/redandroid:text1111/TextViewandroid:layout_width100dpandroid:layout_height50dpandroid:gravitycenterandroid:backgroundcolor/redandroid:text2222/TextViewandroid:layout_width100dpandroid:layout_height50dpandroid:gravitycenterandroid:backgroundcolor/redandroid:text1111/TextViewandroid:layout_width100dpandroid:layout_height50dpandroid:gravitycenterandroid:backgroundcolor/redandroid:text2222//com.learn.customui.custom.FlowLayout/LinearLayout WrapHeightViewPager.java package com.learn.customui.custom;import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup;import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.viewpager.widget.ViewPager;public class WrapHeightViewPager extends ViewPager {public WrapHeightViewPager(NonNull Context context) {super(context);}public WrapHeightViewPager(NonNull Context context, Nullable AttributeSet attrs) {super(context, attrs);}Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);/*** 方法1在super前改变heightMeasureSpec中sizetargetSDK Android33此方法不使用获取到的childnulltargetSDK28 可用其余版本未测试* 1.如何改变MeasureSpec中的size使用MeasureSpec.makeMeasureSpec(size,mode)重新生成一个。* 2.获取高度mode的方法使用MeasureSpec.getMode(heightMeasureSpec)/设置为固定的MeasureSpec.EXACTLY** 方法2在super后重新设置高度不存在方法1的问题setMeasuredDimension(getMeasuredWidth(),height);* 问题* 1.宽度使用 getMeasuredWidth()已在super中设置过。** 共同问题* 1.高度size怎么获取child中最高的* 循环遍历child使用所有child中高度最高的适合child高度统一的情况* 2.当前child的高度如何获取当前child切换child的时候会重新onMeasure()吗当child高度不统一需要再切换的时候重新设置时待验证会有很多坑* getChildAt(getCurrentItem())获取当前child注意切换的时候会销毁其他注意越界问题。* 如果不会重新onMeasure()可以自定义方法自行刷新。*///此处使用方法2实现for (int i 0; i getChildCount(); i) {View child getChildAt(i);ViewGroup.LayoutParams lp child.getLayoutParams();int childWidthMeasureSpec getChildMeasureSpec(widthMeasureSpec, getPaddingLeft() getPaddingRight(), lp.width);int childHeightMeasureSpec getChildMeasureSpec(heightMeasureSpec, getPaddingTop() getPaddingBottom(), lp.height);child.measure(childWidthMeasureSpec,childHeightMeasureSpec);}int height 0;int heightSize MeasureSpec.getSize(heightMeasureSpec);int heightMode MeasureSpec.getMode(heightMeasureSpec);switch (heightMode){case MeasureSpec.EXACTLY:height heightSize;break;case MeasureSpec.AT_MOST:case MeasureSpec.UNSPECIFIED:for (int i 0; i getChildCount(); i) {View child getChildAt(i);height Math.max(height,child.getMeasuredHeight());}break;}setMeasuredDimension(getMeasuredWidth(),height);} }
http://www.w-s-a.com/news/885135/

相关文章:

  • 网站建设工程设计图建网站怎样往网站传视频
  • 做网站月入企业网站建设运营
  • 网站建设中的ftp地址公众号微官网
  • 手机wap网站开发与设计app开发公司电话
  • 网站页脚代码大沥网站开发
  • 重庆网站制作公司 广州天成网络技术有限公司
  • 佛山网站改版wordpress 是否有后门
  • 如何承接网站建设外包wordpress产品布局
  • 洛阳建站洛阳市网站建设视觉设计专业
  • 婚恋网站建设分析网站建设硬件需求
  • 北京做网站电话wordpress如何换图片
  • 电影网站做cpa深圳信息网
  • 单县网站建设优化大师电脑版官网
  • 番禺区住房和建设局物业网站浦东新区网站设计
  • 外贸网站外包WordPress仿牌
  • 如何设计网站logohtml5开发
  • 金坛建设银行总行网站网站开发费用如何记账
  • 贵阳企业网站设计制作湛江知名网站建设电话
  • 网站建设安全性高清效果图网站
  • 上海网站排名推广黄山公司做网站
  • 全国网站建设公司实力排名单页面网站建设
  • 网站建设方案 规划wordpress 要备案吗
  • 一个完整的网站 技术网站建设中 敬请期待.
  • 如何建一个公司的网站网上怎么推广公司产品
  • 十大旅游电子商务网站影楼网站制作
  • 深圳网站建设代理商网业打开慢的原因
  • 旅游网站经营模式在屈臣氏做网站运营
  • 做管理信息的网站com域名查询
  • 免费推广网站推荐外贸推广平台哪个好
  • 腾宁科技做网站399元全包企业校园网站建设