开始播放播放器-ylmf 系统

layoutparams
2023年4月4日发(作者:镜像浏览器)

android⾃动换⾏的线性布局

在项⽬中,有时候会有“横向排列,排满后⾃动换⾏”的需求(⽐如下图),要是⼦view是定长的就没什么好说的了,但如果是变长的话呢?

这篇博客会帮你应对这种需求。

基本思路

1.最外层⼀层竖直线性布局(我们称为⽗布局)

2.新建⽔平线性布局(我们称为⾏布局)

3.计算待放⼊的view的宽度和⾏布局的剩下宽度

4.判断是否可以放⼊

(1).若view的宽度⼩于等于剩余宽度,放⼊,到第三步;

(2).若view的宽度⼤于剩余宽度,添加⾏布局到⽗布局,到第⼆步。

注意

这⾥要注意⼏点:

1.⼦view的宽度要加上间隔;

2.若是⼦view的宽度⼤于⾏布局的宽度,不考虑对⼦view进⼊换⾏,直接放⼊;

接下来看代码,注释已经很详细,就不累赘了。

ty的布局

2.⼦view

3.⼦view的背景

ty代码

<?xmlversion="1.0"encoding="utf-8"?>

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#eee"

>

android:id="@+id/ll_parent"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

/>

<?xmlversion="1.0"encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:textSize="16sp"

android:textColor="@android:color/white"

android:gravity="center"

android:paddingTop="5dp"

android:paddingBottom="5dp"

android:paddingLeft="8dp"

android:paddingRight="8dp"

android:background="@drawable/tv_bg"

/>

<?xmlversion="1.0"encoding="utf-8"?>

publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

te(savedInstanceState);

setContentView(ty_main);

initData();

ll_parent=(LinearLayout)findViewById(_parent);

initAutoLL();

}

//数据

ArrayListdatas=newArrayList<>();

//初始化数据

privatevoidinitData(){

("作家");

("段⼦⼿");

("软⽂作者");

("摄影爱好者");

("画家");

("画家");

("哦我还很喜欢⾳乐");

("还有其他七七⼋⼋的我就不说了");

("⽼师");

}

//最外层的竖直线性布局

privateLinearLayoutll_parent;

//绘制⾃动换⾏的线性布局

privatevoidinitAutoLL(){

//每⼀⾏的布局,初始化第⼀⾏布局

LinearLayoutrowLL=newLinearLayout(this);

ParamsrowLP=

Params(_PARENT,

_CONTENT);

floatrowMargin=dipToPx(10);

gins(0,(int)rowMargin,0,0);

outParams(rowLP);

booleanisNewLayout=false;

floatmaxWidth=getScreenWidth()-dipToPx(30);

//剩下的宽度

floatelseWidth=maxWidth;

ParamstextViewLP=

Params(_CONTENT,

_CONTENT);

gins((int)dipToPx(8),0,0,0);

for(inti=0;i<();i++){

//若当前为新起的⼀⾏,先添加旧的那⾏

//然后重新创建布局对象,设置参数,将isNewLayout判断重置为false

if(isNewLayout){

ll_w(rowLL);

rowLL=newLinearLayout(this);

outParams(rowLP);

isNewLayout=false;

}

//计算是否需要换⾏

TextViewtextView=(TextView)getLayoutInflater().inflate(ew,null);

t((i));

e(0,0);

//若是⼀整⾏都放不下这个⽂本框,添加旧的那⾏,新起⼀⾏添加这个⽂本框

if(maxWidth

ll_w(rowLL);

rowLL=newLinearLayout(this);

outParams(rowLP);

w(textView);

isNewLayout=true;

continue;

}

//若是剩下的宽度⼩于⽂本框的宽度(放不下了)

//添加旧的那⾏,新起⼀⾏,但是i要-1,因为当前的⽂本框还未添加

if(elseWidth

isNewLayout=true;

i--;

//重置剩余宽度

elseWidth=maxWidth;

continue;

}else{

//剩余宽度减去⽂本框的宽度+间隔=新的剩余宽度

elseWidth-=suredWidth()+dipToPx(8);

if(ldCount()==0){

w(textView);

}else{

outParams(textViewLP);

w(textView);

}

其中主要代码在initAutoLL函数内部,运⾏后便可看到⽂章开头处的效果图

}

}

}

//添加最后⼀⾏,但要防⽌重复添加

ll_View(rowLL);

ll_w(rowLL);

}

//dp转px

privatefloatdipToPx(intdipValue){

imension(X_UNIT_DIP,

dipValue,

ources().getDisplayMetrics());

}

//获得评论宽度

privatefloatgetScreenWidth(){

ources().getDisplayMetrics().widthPixels;

}

}

更多推荐

layoutparams