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消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

lvgl8.3.6 控件垂直布局 label控件在image控件的下方显示

在使用 LVGL 8.3.6 创建一个垂直布局,其中 label 控件位于 image 控件下方,你可以使用 lv_obj_set_flex_flow 来设置布局为垂直,并确保 label 控件在 image 控件后添加。这里是如何步骤性地实现它的一个基本示例: 创建父容器:首先创建一个容器对象,该对象将作为布局的基础。设置容器为垂直布局:使用 lv_obj_set_flex_flow 设置容器

HTML5自定义属性对象Dataset

原文转自HTML5自定义属性对象Dataset简介 一、html5 自定义属性介绍 之前翻译的“你必须知道的28个HTML5特征、窍门和技术”一文中对于HTML5中自定义合法属性data-已经做过些介绍,就是在HTML5中我们可以使用data-前缀设置我们需要的自定义属性,来进行一些数据的存放,例如我们要在一个文字按钮上存放相对应的id: <a href="javascript:" d

一步一步将PlantUML类图导出为自定义格式的XMI文件

一步一步将PlantUML类图导出为自定义格式的XMI文件 说明: 首次发表日期:2024-09-08PlantUML官网: https://plantuml.com/zh/PlantUML命令行文档: https://plantuml.com/zh/command-line#6a26f548831e6a8cPlantUML XMI文档: https://plantuml.com/zh/xmi

Apache Tiles 布局管理器

陈科肇 =========== 1.简介 一个免费的开源模板框架现代Java应用程序。  基于该复合图案它是建立以简化的用户界面的开发。 对于复杂的网站,它仍然最简单,最优雅的方式来一起工作的任何MVC技术。 Tiles允许作者定义页面片段可被组装成在运行一个完整的网页。  这些片段,或Tiles,可以用于为了降低公共页面元素的重复,简单地包括或嵌入在其它瓦片,制定了一系列可重复使用