垂直布局
水平布局
阶梯布局
先创建自定义布局的类 MyLinearLayout
public class MyLinearLayout extends ViewGroup{
private int marginTop = 20;
private int marginLeft = 20;
private int initTop = 20;
private int initLeft = 20;
private int orientation;//0或1或2
public MyLinearLayout(Context context) {
this(context,null);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
//最终的宽和高
int totalHeight = 0;
int totalWidth = 0;
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
if(orientation==0){//0为垂直 1为水平 2为阶梯
switch (modeHeight){
case MeasureSpec.EXACTLY:
totalHeight = sizeHeight;
totalWidth = sizeWidth;
break;
case MeasureSpec.AT_MOST:
for (int i=0;i<getChildCount();i++){
View childAt = getChildAt(i);
totalHeight += childAt.getMeasuredHeight()+marginTop;
}
totalHeight+=initTop;
totalWidth = getChildAt(0).getMeasuredWidth()+marginLeft*2;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
}else if(orientation==1){//0为垂直 1为水平 2为阶梯
switch (modeWidth){
case MeasureSpec.EXACTLY:
totalWidth = sizeWidth;
totalHeight = sizeHeight;
break;
case MeasureSpec.AT_MOST:
for (int i=0;i<getChildCount();i++){
View childAt = getChildAt(i);
totalWidth += childAt.getMeasuredWidth()+marginLeft;
// totalWidth = childAt.getMeasuredWidth()+marginLeft;
}
totalWidth+=initLeft;
totalHeight = getChildAt(0).getMeasuredHeight()+marginTop*2;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
}else if(orientation==2){//0为垂直 1为水平 2为阶梯
switch (modeHeight){
case MeasureSpec.EXACTLY:
totalHeight = sizeHeight;
totalWidth = sizeWidth;
break;
case MeasureSpec.AT_MOST:
for (int i=0;i<getChildCount();i++){
View childAt = getChildAt(i);
totalHeight += childAt.getMeasuredHeight()+marginTop;
}
totalHeight+=initTop;
// totalWidth = getChildAt(0).getMeasuredWidth()+marginLeft*2;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
switch (modeWidth){
case MeasureSpec.EXACTLY:
totalWidth = sizeWidth;
totalHeight = sizeHeight;
break;
case MeasureSpec.AT_MOST:
for (int i=0;i<getChildCount();i++){
View childAt = getChildAt(i);
totalWidth += childAt.getMeasuredWidth()+marginLeft;
// totalWidth = childAt.getMeasuredWidth()+marginLeft;
}
totalWidth+=initLeft;
// totalHeight = getChildAt(0).getMeasuredHeight()+marginTop*2;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
}
setMeasuredDimension(totalWidth,totalHeight);
}
/**
* 必须重写的方法..确定view的位置
* */
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int mTop = initTop;
int mLeft = initLeft;
int childCount = getChildCount();//拿到子控件的数量
for (int i=0;i<childCount;i++){
View childAt = getChildAt(i);//循环遍历中拿到当前的view
// Log.e("myMessage","mTop = "+mTop);
//此时是0,因为没有写onmeasure,,measureChildren(widthMeasureSpec,heightMeasureSpec);
// childAt.layout(0,0,childAt.getMeasuredWidth(),childAt.getMeasuredHeight());
//不重叠,拉开上下距离
childAt.layout(mLeft,mTop,childAt.getMeasuredWidth()+mLeft,childAt.getMeasuredHeight()+mTop);
if(orientation==0){//0为垂直
mTop+=childAt.getMeasuredHeight()+marginTop;
}else if(orientation==1){//1为水平
mLeft+=childAt.getMeasuredWidth()+marginLeft;
} if(orientation==2){//2为阶梯,斜对角
mTop+=childAt.getMeasuredHeight()+marginTop;
mLeft+=childAt.getMeasuredWidth()+marginLeft;
}
}
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyLinearLayout);
//orientation = 0,1,2 此时拿到了值
orientation = typedArray.getInteger(0,0);
}
}
在values下新建attrs.xml (values-new-xml-values xml file)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyLinearLayout">
<attr name="orientation" format="enum">
<enum value="0" name="vertical"/>
<enum value="1" name="horizontal"/>
<enum value="2" name="all"/>
</attr>
<attr name="orientation1" format="integer"/>
</declare-styleable>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:gravity="center"
tools:context="com.example.day180110_customlinearlayout.MainActivity">
<com.example.day180110_customlinearlayout.customview.MyLinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#a3e4d9"
app:orientation="all"
>
<TextView
android:gravity="center"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#99dfc0ea"
android:text="第一" />
<TextView
android:gravity="center"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#99d599ea"
android:text="第二" />
<TextView
android:gravity="center"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#99c66ce6"
android:text="第三" />
<TextView
android:gravity="center"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#99bb43e7"
android:text="第四" />
</com.example.day180110_customlinearlayout.customview.MyLinearLayout>
</LinearLayout>