ACRA 和自定义布局

2023-10-19 00:30
文章标签 自定义 布局 acra

本文主要是介绍ACRA 和自定义布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ACRA : Application crash report for android

  1. 作用: 为自己的应用找bug
  2. 使用步骤:参考文档

自定义布局的实现:流程图

参照流程图:当有孩子时,是否需要对孩子控件大小进行布置,如果需要就得重写onMeasure()这个方法调用child.layout()方法。需要孩子控件布局进行控制也要重写onLayout()方法,需要对控件的显示进行控制时要重写onDraw()方法。

一般实现全部构造函数。

重写

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{//临时topint topmt=0;int parentWidth = getMeasuredWidth();//获得孩子个数int count =getChildCount();for(int i=0;i<count;i++){//获得孩子实例View child=getChildAt(i);//获得孩子的高度、宽度int childHidth=child.getMeasuredHeight();int childWidth=child.getMeasuredWidth();if(i%2==0){ //双行int left=parentWidth-childWidth;int top=topmt;int right=left+childWidth;int bottom=top+childHidth;child.layout(left, top, right, bottom);}else{//单行int left=0;int top=topmt;int right=left+childWidth;int bottom=top+childHidth;child.layout(left, top, right, bottom);}topmt+=childHidth;}
}

还需要重写onMeasure()

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{//获得父亲的宽高int widthsize=MeasureSpec.getSize(widthMeasureSpec);int heightsize=MeasureSpec.getSize(heightMeasureSpec);measureChildren(0, 0);//设置孩子,为0由父亲安排宽高setMeasuredDimension(widthsize, heightsize);
}

自定义布局的使用:

<com.cca.definelayout.CustomerLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:id="@+id/customer_layout"android:layout_height="match_parent"tools:context="com.cca.definelayout.MainActivity" ><View android:layout_width="220dp"android:layout_height="40dp"android:background="#ff0000"/><View android:layout_width="220dp"android:layout_height="40dp"android:background="#00ff00"/><View android:layout_width="220dp"android:layout_height="40dp"android:background="#fe3300"/><View android:layout_width="220dp"android:layout_height="40dp"android:background="#ff0220"/><View android:layout_width="220dp"android:layout_height="40dp"android:background="#ffdd00"/>
</com.cca.definelayout.CustomerLayout>

如果需要切换布局,一般定义一个boolean类型值进行切换

FlowLayout

  1. 分析:
    1. 多行摆放
    2. 单行如果摆不下去,换行摆放

看下效果图:

一下子看到这种布局说真的我无从下手,不知道这个怎么实现,每行个数不一样,列数不一样,后来知道自定义布局可以实现。根据上面的view实现的流程图可以知道,需要对控件的布局、大小进行控制,所以需要重写onLayout()、onMeasure()的方法。再重写方法之前,需要先分析每一行是怎么布局的,以面向对象的思想封装每一行需要的属性、和方法。如下:

行的类

    class Line{//存储孩子private List<View> mChildViews=new LinkedList<View>();private int usedWidth;//已经使用过的宽度private int lineHeight;//行最大的高度private int maxWidth;//行最大的宽度,父类给的private int horizontalSpace;//中间的间隔public Line(int maxWidth,int horizontalSpace){this.maxWidth=maxWidth;this.horizontalSpace=horizontalSpace;}//判断该行是否能添加viewpublic boolean canAddView(View view){//如果使用的宽度+准备加的View的宽度+中间的间隔>最大的宽度,加不上去//准备加的View的宽度int childwidth=view.getMeasuredWidth();int size=mChildViews.size();if(size==0){return true;}else if(usedWidth+childwidth+horizontalSpace>maxWidth){return false;}return true;}//添加view到布局public void addView(View view){//int childWidth=view.getMeasuredWidth();int childHeight=view.getMeasuredHeight();int size=mChildViews.size();if(size==0){//没有孩子      已经使用的宽度if(childWidth>maxWidth){usedWidth=maxWidth;}else{usedWidth=childWidth;}//高度lineHeight=childHeight;}else{//已经使用的宽度usedWidth=usedWidth+childWidth+horizontalSpace;//高度lineHeight=lineHeight>childHeight?lineHeight:childHeight;}//加孩子mChildViews.add(view);}//给行布局public void layout(int left,int top){//给孩子布局int size=mChildViews.size();int tmpLeft=0;//将每一行右侧无法显示的空白部分平分给每一行显示的每个控件int extraWidth=(int) ((maxWidth-usedWidth)*1f/size+0.5f);for(int i=0;i<size;i++){View child=mChildViews.get(i);int childWidth=child.getMeasuredWidth();int childHeight=child.getMeasuredHeight();if(extraWidth>0){//希望孩子再宽点,填充右侧空白int widthMeasureSpec=MeasureSpec.makeMeasureSpec(childWidth+extraWidth, MeasureSpec.EXACTLY);int heightMeasureSpec=MeasureSpec.makeMeasureSpec(childHeight,MeasureSpec.EXACTLY);child.measure(widthMeasureSpec, heightMeasureSpec);//重新获得宽高childWidth=child.getMeasuredWidth();childHeight=child.getMeasuredHeight();}int extraHeight=(int) ((lineHeight-childHeight)/2f+0.5f);int l=left+tmpLeft;int t=top+extraHeight;int r=l+childWidth;int b=t+childHeight;child.layout(l, t, r, b);//添加记录tmpLeft +=childWidth + horizontalSpace;}}
}

重写onLayout()方法:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{int left=getPaddingLeft();int top=getPaddingTop();//让行进行布局for(int i=0;i<mLines.size();i++){Line line=mLines.get(i);//给行布局line.layout(left,top);//添加top的记录top +=line.lineHeight;if(i!=mLines.size()-1){top +=mVertivalSpace;}}}

重写onMeasure()方法

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{//孩子个数记录清空mLines.clear();mCurrentLine=null;int widthSize=MeasureSpec.getSize(widthMeasureSpec);int lineMaxWidth=widthSize-getPaddingLeft()-getPaddingRight();//测量孩子完成时,就记录到行里面int count=getChildCount();for(int i=0;i<count;i++){View child=getChildAt(i);//孩子不可见时if(child.getVisibility()==View.GONE){continue;}//给孩子宽高赋值,父亲给的最大宽高measureChild(child, widthMeasureSpec, heightMeasureSpec);//将孩子添加到行中if(mCurrentLine==null){//新建行mCurrentLine=new Line(lineMaxWidth,mHorizontalSpace);//添加到布局中mLines.add(mCurrentLine);}//给行添加孩子if(mCurrentLine.canAddView(child)){//可以添加孩子mCurrentLine.addView(child);}else{//加不了//换行mCurrentLine=new Line(lineMaxWidth,mHorizontalSpace);//添加到布局中mLines.add(mCurrentLine);//再加孩子mCurrentLine.addView(child);}}//设置自己的宽高int measuredWidth=widthSize;int measuredHeight=getPaddingTop()+getPaddingBottom();//通过line的高来计算自己的高度for(int i=0;i<mLines.size();i++){Line line=mLines.get(i);measuredHeight +=line.lineHeight;if(i!=0){measuredHeight +=mVertivalSpace;    }}setMeasuredDimension(measuredWidth, measuredHeight);
}

在MainActivity中:

public class MainActivity extends Activity {private FlowLayout layout;private String []mDatas={"单机游戏","美女","游戏","单机游戏","美女","淘宝","单机游戏","美女","淘宝","游戏" ,"单机游戏","淘宝","游戏","单机游戏","美女","淘宝","游戏","单机游戏","美女","淘宝","游戏" ,"有道","天猫","汽车商城","新闻","运动","熊出没之大逃跑"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);layout=(FlowLayout) findViewById(R.id.flow_layout);layout.setPadding(10, 10, 10, 10);initData();}private void initData(){for(int i=0;i<mDatas.length;i++){TextView tv=new TextView(this);tv.setText(mDatas[i]);tv.setTextColor(Color.WHITE);tv.setGravity(Gravity.CENTER);tv.setBackgroundColor(Color.GRAY);tv.setPadding(3, 3, 3, 3);layout.addView(tv);}}
}

使用自定义布局时:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.cca.frowlayout.MainActivity" ><com.cca.frowlayout.FlowLayoutandroid:id="@+id/flow_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"/></ScrollView>

到这里,上图的效果就已经出来,这种布局的逻辑判断有点繁琐,稍有差错就显示不出来了。记得要多看。

这篇关于ACRA 和自定义布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/236093

相关文章

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re