Android LinearLayout实现自动换行效果

发布时间:2020-09-13 14:19:34

来源:脚本之家

阅读:431

作者:星辰之力

在我们开发过程中会经常遇见一些客户要求但是Android系统又不提供的效果,这时我们只能自己动手去实现它,或者从网络上借鉴他人的资源,本着用别人不如自己会做的心态,在此我总结了一下Android中如何实现自动换行的LinearLayout。

在本文中,说是LinearLayout其实是继承自GroupView,在这里主要重写了两个方法,onMeasure、onLayout方法,下面我对此加以介绍。(代码中使用了AttributeSet,由于时间问题不再予以介绍)。

1. onMeasure是干什么的?

在ViewGroup的创建过程中,onMeasure是在onLayout之前的,所以在此先对onMeasure进行介绍,onMeasure方法是计算子控件与父控件在屏幕中所占长宽大小的,onMeasure传入两个参数——widthMeasureSpec和heightMeasureSpec. 这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.

int withMode = MeasureSpec.getMode(widthMeasureSpec);

int withSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

Mode有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST,如果是AT_MOST,Size代表的是最大可获得的空间;如果是EXACTLY,Size代表的是精确的尺寸;如果是UNSPECIFIED,就是你想要多少就有多少。经过代码测试就知道,当我们设置width或height为fill_parent时,容器在布局时调用子 view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。而当设置为 wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY。

2. onLayout是干什么的?

与onMesaure相比,onLayout更加容易理解,它的作用就是调座位,就是把所有的子View根据不同的需要,通过View. layout(int l, int t, int r, int b)方法指定它所在的位置。

3. 解决问题

只要对onMeasure和onLayout加以理解,对于该篇所要实现的功能就不再难以实现,下面贴上代码,并在代码中讲解。

WaroLinearLayout.java

public class WarpLinearLayout extends ViewGroup {

private Type mType;

private List mWarpLineGroup;

public WarpLinearLayout(Context context) {

this(context, null);

}

public WarpLinearLayout(Context context, AttributeSet attrs) {

this(context, attrs, R.style.WarpLinearLayoutDefault);

}

public WarpLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

mType = new Type(context, attrs);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int withMode = MeasureSpec.getMode(widthMeasureSpec);

int withSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int with = 0;

int height = 0;

int childCount = getChildCount();

/**

* 在调用childView。getMeasre之前必须先调用该行代码,用于对子View大小的测量

*/

measureChildren(widthMeasureSpec, heightMeasureSpec);

/**

* 计算宽度

*/

switch (withMode) {

case MeasureSpec.EXACTLY:

with = withSize;

break;

case MeasureSpec.AT_MOST:

for (int i = 0; i < childCount; i++) {

if (i != 0) {

with += mType.horizontal_Space;

}

with += getChildAt(i).getMeasuredWidth();

}

with += getPaddingLeft() + getPaddingRight();

with = with > withSize ? withSize : with;

break;

case MeasureSpec.UNSPECIFIED:

for (int i = 0; i < childCount; i++) {

if (i != 0) {

with += mType.horizontal_Space;

}

with += getChildAt(i).getMeasuredWidth();

}

with += getPaddingLeft() + getPaddingRight();

break;

default:

with = withSize;

break;

}

/**

* 根据计算出的宽度,计算出所需要的行数

*/

WarpLine warpLine = new WarpLine();

/**

* 不能够在定义属性时初始化,因为onMeasure方法会多次调用

*/

mWarpLineGroup = new ArrayList();

for (int i = 0; i < childCount; i++) {

if (warpLine.lineWidth + getChildAt(i).getMeasuredWidth() + mType.horizontal_Space > with) {

if (warpLine.lineView.size() == 0) {

warpLine.addView(getChildAt(i));

mWarpLineGroup.add(warpLine);

warpLine = new WarpLine();

} else {

mWarpLineGroup.add(warpLine);

warpLine = new WarpLine();

warpLine.addView(getChildAt(i));

}

} else {

warpLine.addView(getChildAt(i));

}

}

/**

* 添加最后一行

*/

if (warpLine.lineView.size() > 0 && !mWarpLineGroup.contains(warpLine)) {

mWarpLineGroup.add(warpLine);

}

/**

* 计算宽度

*/

height = getPaddingTop() + getPaddingBottom();

for (int i = 0; i < mWarpLineGroup.size(); i++) {

if (i != 0) {

height += mType.vertical_Space;

}

height += mWarpLineGroup.get(i).height;

}

switch (heightMode) {

case MeasureSpec.EXACTLY:

height = heightSize;

break;

case MeasureSpec.AT_MOST:

height = height > heightSize ? heightSize : height;

break;

case MeasureSpec.UNSPECIFIED:

break;

default:

break;

}

setMeasuredDimension(with, height);

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

t = getPaddingTop();

for (int i = 0; i < mWarpLineGroup.size(); i++) {

int left = getPaddingLeft();

WarpLine warpLine = mWarpLineGroup.get(i);

int lastWidth = getMeasuredWidth() - warpLine.lineWidth;

for (int j = 0; j < warpLine.lineView.size(); j++) {

View view = warpLine.lineView.get(j);

if (isFull()) {//需要充满当前行时

view.layout(left, t, left + view.getMeasuredWidth() + lastWidth / warpLine.lineView.size(), t + view.getMeasuredHeight());

left += view.getMeasuredWidth() + mType.horizontal_Space + lastWidth / warpLine.lineView.size();

} else {

switch (getGrivate()) {

case 0://右对齐

view.layout(left + lastWidth, t, left + lastWidth + view.getMeasuredWidth(), t + view.getMeasuredHeight());

break;

case 2://居中对齐

view.layout(left + lastWidth / 2, t, left + lastWidth / 2 + view.getMeasuredWidth(), t + view.getMeasuredHeight());

break;

default://左对齐

view.layout(left, t, left + view.getMeasuredWidth(), t + view.getMeasuredHeight());

break;

}

left += view.getMeasuredWidth() + mType.horizontal_Space;

}

}

t += warpLine.height + mType.vertical_Space;

}

}

/**

* 用于存放一行子View

*/

private final class WarpLine {

private List lineView = new ArrayList();

/**

* 当前行中所需要占用的宽度

*/

private int lineWidth = getPaddingLeft() + getPaddingRight();

/**

* 该行View中所需要占用的最大高度

*/

private int height = 0;

private void addView(View view) {

if (lineView.size() != 0) {

lineWidth += mType.horizontal_Space;

}

height = height > view.getMeasuredHeight() ? height : view.getMeasuredHeight();

lineWidth += view.getMeasuredWidth();

lineView.add(view);

}

}

/**

* 对样式的初始化

*/

private final static class Type {

/*

*对齐方式 right 0,left 1,center 2

*/

private int grivate;

/**

* 水平间距,单位px

*/

private float horizontal_Space;

/**

* 垂直间距,单位px

*/

private float vertical_Space;

/**

* 是否自动填满

*/

private boolean isFull;

Type(Context context, AttributeSet attrs) {

if (attrs == null) {

return;

}

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WarpLinearLayout);

grivate = typedArray.getInt(R.styleable.WarpLinearLayout_grivate, grivate);

horizontal_Space = typedArray.getDimension(R.styleable.WarpLinearLayout_horizontal_Space, horizontal_Space);

vertical_Space = typedArray.getDimension(R.styleable.WarpLinearLayout_vertical_Space, vertical_Space);

isFull = typedArray.getBoolean(R.styleable.WarpLinearLayout_isFull, isFull);

}

}

public int getGrivate() {

return mType.grivate;

}

public float getHorizontal_Space() {

return mType.horizontal_Space;

}

public float getVertical_Space() {

return mType.vertical_Space;

}

public boolean isFull() {

return mType.isFull;

}

public void setGrivate(int grivate) {

mType.grivate = grivate;

}

public void setHorizontal_Space(float horizontal_Space) {

mType.horizontal_Space = horizontal_Space;

}

public void setVertical_Space(float vertical_Space) {

mType.vertical_Space = vertical_Space;

}

public void setIsFull(boolean isFull) {

mType.isFull = isFull;

}

/**

* 每行子View的对齐方式

*/

public final static class Gravite {

public final static int RIGHT = 0;

public final static int LEFT = 1;

public final static int CENTER = 2;

}

}

attrs.xml

WarpLinearLayoutDefault

left

20dp

20dp

false

MainActivity.java

public class MainActivity extends Activity {

private Button btn;

private WarpLinearLayout warpLinearLayout;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.btn);

warpLinearLayout = (WarpLinearLayout) findViewById(R.id.warpLinearLayout);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int n = new Random().nextInt(10) + 5;

StringBuffer stringBuffer = new StringBuffer();

Random random = new Random();

Log.i("WarpLinearLayout","n="+n);

for (int i = 0; i < n; i++) {

stringBuffer.append((char)(65+random.nextInt(26)));

Log.i("WarpLinearLayout", "StringBuffer=" + stringBuffer.toString());

}

TextView tv = new TextView(MainActivity.this);

tv.setText(stringBuffer.toString()+"000");

tv.setBackgroundResource(R.drawable.radius_backgroup_yellow);

tv.setPadding(10,10,10,10);

warpLinearLayout.addView(tv);

}

});

}

}

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity">

android:id="@+id/btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="add"

android:textSize="20dp" />

android:id="@+id/warpLinearLayout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/btn"

android:background="#FF00FF00"

android:padding="10dp"

app:grivate="right"

app:horizontal_Space="10dp"

app:isFull="false"

app:vertical_Space="10dp">

运行效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

android 自动换行linearlayout,Android LinearLayout实现自动换行效果相关推荐

  1. android 按钮换行_Android LinearLayout实现自动换行

    由于前段时间项目中使用到了自动换行的线性布局,本来打算用表格布局在里面一个个的用Java代码添加ImageView的,但是添加的View控件是不确定的,因为得靠服务器的数据返回,就这样手动用Java代 ...

  2. android背景气泡,android之View跟LinearLayout的重写(实现背景气泡和波纹效果)

    android之View和LinearLayout的重写(实现背景气泡和波纹效果) 前两天看了仿android L里面水波纹效果的两篇博客 Android L中水波纹点击效果的实现 Android自定 ...

  3. 具有自动换行功能的LinearLayout

    自定义自动换行的LinearLayout 1 MyAutoLineFeedLinearLayout 是用来承载内容的父控件,定义代码如下: package com.gsww.hzz.uikit.vie ...

  4. Android 使用线性布局LinearLayout和Button实现一个点红块游戏

    这个游戏的功能类似打地鼠. 项目地址:https://github.com/moonlightpoet/RedBlock 程序下载试玩地址:https://github.com/moonlightpo ...

  5. Android UI布局之LinearLayout

    LinearLayout是Android中最经常使用的布局之中的一个.它将自己包括的子元素依照一个方向进行排列.方向有两种,水平或者竖直.这个方向能够通过设置android:orientation=& ...

  6. android 消息列表,[Android]用LinearLayout 实现类微信消息列表项

    实现效果: 微信参照界面: 一个关键点: "早晚报"与"下午19:05"之间要用空白TextView隔开,来造成一个靠左,一个靠右的效果 LinearLayou ...

  7. android 布局排排,[android]如何使LinearLayout布局从右向左水平排列,而不是从左向右排列...

    方法1:利用android:layout_weight android:layout_width="match_parent" android:layout_height=&quo ...

  8. android shape 底部线,android用shape给linearLayout设置边框,怎样只保留底部或顶部的边框,把其它三个方向的边框去掉呢?...

    http://bbs.csdn.net/topics/390485215 这种方法只是两个颜色块相减而已 android:top="1dp" android:left=" ...

  9. android linearlayout 方法,Android布局控件-LinearLayout详解

    1.线性布局 LinearLayout LinearLayout简单来说就是线性布局,线性肯定是具有横竖两种方向的,水平和垂直. 在使用LinearLayout的时候,需要注意以下几点 2.排列方式( ...

最新文章

  1. 于媛龄(201552118)第二次作业网调问卷的制作
  2. 使用nmap-converter将nmap扫描结果XML转化为XLS实战
  3. 卷进大厂系列之LeetCode刷题笔记:长度最小的子数组(中等)
  4. 基本概念学习(8003)---CPU中央处理器
  5. tableau必知必会之妙用 Lookup 函数同时跨行跨列取数
  6. 由CloudStack项目引起的ESXI嵌套虚拟化引起的二级虚拟机无法被访问
  7. 漫画:数据中台中台建设的十大误区(建议收藏)
  8. apache2.4打开laravel项目_Laravel异步队列全攻略
  9. 《TensorFlow 2.0深度学习算法实战教材》学习笔记(四、TensorFlow 进阶)
  10. java encode乱码_java 中文乱码问题的解决
  11. 安川ga700变频器故障码集_安川变频器故障显示代码
  12. 2019莆田学院c语言试卷,莆田学院C语言程序设计模拟试卷_文库吧
  13. 微信登陆失败redirect_uri 域名与后台配置不一致 10003(thinkphp)
  14. Github没有Download Zip(下载zip)的绿色选项是因为所在的是一个子目录
  15. 一个商人骑一头驴要穿越1000公里长的沙漠,去卖3000根胡萝卜。已知驴一次性可驮1000根胡萝卜,但每走一公里又要吃掉一根胡萝卜。问:商人共可卖出多少胡萝卜? 答:533
  16. 小萌谈Art(3)——离线编程篇
  17. iOS UIcollectionview 数据量少时候bounces无效的解决方法
  18. miui12是Android版本,miui12基于安卓几版本开发的?miui12是安卓11吗[多图]
  19. 使用WebBrowser控件实现打印 去掉 页眉和页脚
  20. 医院PACS系统源码,PACS源码,带3D重建PACS系统源码

热门文章

  1. html td强制不换行显示,CSS实现DIV或者TD中强制不换行
  2. 软考归来~分享一下历程和心得体会
  3. 网络篇 EIGRP协议-27
  4. 基于EMG的正向动力学方法基本流程
  5. C++期末复习超详细总结知识点(期末冲刺)
  6. linux mount 默认端口,CentOS7下NFS服务安装及配置固定端口
  7. 手机淘宝APP主要视觉设计图分析【惠龙之 :花骨朵儿】
  8. 如何调教一个定制化的ChatGPT私人助理,接入自家知识库
  9. 山东省普通高中计算机考试成绩,山东省学业水平考试成绩查询系统_山东省普通高中学业水平成绩查询 山东省高中学业水平考试成绩查询系统...
  10. Openstack云环境的登录和基本使用