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

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor