AndroidLinearLayout实现⾃动换⾏效果
在我们开发过程中会经常遇见⼀些客户要求但是Android系统⼜不提供的效果,这时我们只能⾃⼰动⼿去实现它,或者从⽹络上借鉴他⼈的资源,本着⽤别⼈不如⾃⼰会做的⼼态,在此我总结了⼀下Android中如何实现⾃动换⾏的LinearLayout。
在本⽂中,说是LinearLayout其实是继承⾃GroupView,在这⾥主要重写了两个⽅法,onMeasure、onLayout⽅法,下⾯我对此加以介绍。(代码中使⽤了AttributeSet,由于时间问题不再予以介绍)。
1. onMeasure是⼲什么的?
在ViewGroup的创建过程中,onMeasure是在onLayout之前的,所以在此先对onMeasure进⾏介绍,onMeasure⽅法是计算⼦控件与⽗控件在屏幕中所占长宽⼤⼩的,onMeasure传⼊两个参数——widthMeasureSpec和heightMeasureSpec. 这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.
int withMode = Mode(widthMeasureSpec);
int withSize = Size(widthMeasureSpec);
int heightMode = Mode(heightMeasureSpec);
int heightSize = Size(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<WarpLine> 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 = Mode(widthMeasureSpec);
int withSize = Size(widthMeasureSpec);
int heightMode = Mode(heightMeasureSpec);
int heightSize = Size(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<WarpLine>();
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();
android layout布局warpLine.addView(getChildAt(i));
}
} else {
warpLine.addView(getChildAt(i));
}
}
/**
* 添加最后⼀⾏
*/
if (warpLine.lineView.size() > 0 && !ains(warpLine)) {
mWarpLineGroup.add(warpLine);
}
/**
* 计算宽度
*/
height = getPaddingTop() + getPaddingBottom();
for (int i = 0; i < mWarpLineGroup.size(); i++) {
if (i != 0) {
height += mType.vertical_Space;
}
height += (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 = (i);
int lastWidth = getMeasuredWidth() - warpLine.lineWidth;
for (int j = 0; j < warpLine.lineView.size(); j++) {
View view = (j);
if (isFull()) {//需要充满当前⾏时
view.layout(left, t, left + MeasuredWidth() + lastWidth / warpLine.lineView.size(), t + MeasuredHeight()); left += MeasuredWidth() + mType.horizontal_Space + lastWidth / warpLine.lineView.size();
} else {
switch (getGrivate()) {
case 0://右对齐
view.layout(left + lastWidth, t, left + lastWidth + MeasuredWidth(), t + MeasuredHeight());
break;
case 2://居中对齐
view.layout(left + lastWidth / 2, t, left + lastWidth / 2 + MeasuredWidth(), t + MeasuredHeight());
break;
default://左对齐
view.layout(left, t, left + MeasuredWidth(), t + MeasuredHeight());
break;
}
left += MeasuredWidth() + mType.horizontal_Space;
}
}
t += warpLine.height + mType.vertical_Space;
}
}
/**
* ⽤于存放⼀⾏⼦View
*/
private final class WarpLine {
private List<View> lineView = new ArrayList<View>();
/**
* 当前⾏中所需要占⽤的宽度
*/
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 > MeasuredHeight() ? height : MeasuredHeight();
lineWidth += MeasuredWidth();
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 = Int(R.styleable.WarpLinearLayout_grivate, grivate);
horizontal_Space = Dimension(R.styleable.WarpLinearLayout_horizontal_Space, horizontal_Space); vertical_Space = Dimension(R.styleable.WarpLinearLayout_vertical_Space, vertical_Space);
isFull = Boolean(R.styleable.WarpLinearLayout_isFull, isFull);
}
}
public int getGrivate() {
ivate;
}
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) {
}
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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="WarpLinearLayout">
<attr name="grivate" format="enum"><!--对齐⽅式 !-->
<enum name="right" value="0"></enum>
<enum name="left" value="1"></enum>
<enum name="center" value="2"></enum>
</attr>
<attr name="horizontal_Space" format="dimension"></attr>
<attr name="vertical_Space" format="dimension"></attr>
<attr name="isFull" format="boolean"></attr>
</declare-styleable>
</resources>
WarpLinearLayoutDefault
<style name="WarpLinearLayoutDefault">
<item name="grivate">left</item>
<item name="horizontal_Space">20dp</item>
<item name="vertical_Space">20dp</item>
<item name="isFull">false</item>
</style>
MainActivity.java
public class MainActivity extends Activity {
private Button btn;
private WarpLinearLayout warpLinearLayout;
@Override
protected void onCreate(Bundle 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)(Int(26)));
Log.i("WarpLinearLayout", "StringBuffer=" + String());
}
TextView tv = new TextView(MainActivity.this);
tv.String()+"000");
tv.setBackgroundResource(R.drawable.radius_backgroup_yellow);
tv.setPadding(10,10,10,10);
warpLinearLayout.addView(tv);
}
});
}
}
l
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
xmlns:app="schemas.android/apk/res-auto"
xmlns:tools="schemas.android/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">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="add"
android:textSize="20dp" />
&le.customview.viewgroup.WarpLinearLayout
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"></ample.customview.viewgroup.WarpLinearLayout> </RelativeLayout>
运⾏效果图如下:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论