您的位置:首页 > 产品设计 > UI/UE

Android-UI布局---onLayout、layout方法

2015-11-16 19:14 666 查看
onLayout方法是ViewGroup中子View的布局方法,用于放置子View的位置。放置子View很简单,只需在重写onLayout方法,然后获取子View的实例,调用子View的layout方法实现布局。在实际开发中,一般要配合onMeasure测量方法一起使用。

onLayout方法:

@Override
protected abstract void onLayout(boolean changed,
int l, int t, int r, int b);


该方法在ViewGroup中定义是抽象函数,继承该类必须实现onLayout方法,而ViewGroup的onMeasure并非必须重写的。View的放置都是根据一个矩形空间放置的,onLayout传下来的l,t,r,b分别是放置父控件的矩形可用空间(除去margin和padding的空间)的左上角的left、top以及右下角right、bottom值。

layout方法:

public void layout(int l, int t, int r, int b);


该方法是View的放置方法,在View类实现。调用该方法需要传入放置View的矩形空间左上角left、top值和右下角right、bottom值。这四个值是相对于父控件而言的。例如传入的是(10, 10, 100, 100),则该View在距离父控件的左上角位置(10, 10)处显示,显示的大小是宽高是90(参数r,b是相对左上角的),这有点像绝对布局。

平常开发所用到RelativeLayout、LinearLayout、FrameLayout...这些都是继承ViewGroup的布局。这些布局的实现都是通过都实现ViewGroup的onLayout方法,只是实现方法不一样而已。

下面是一个自定义ViewGroup的Demo,用onLayout和layout实现子View的水平放置,间隔是20px

public class MyViewGroup extends ViewGroup {

// 子View的水平间隔
private final static int padding = 20;

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub

// 动态获取子View实例
for (int i = 0, size = getChildCount(); i < size; i++) {
View view = getChildAt(i);
// 放置子View,宽高都是100
view.layout(l, t, l + 100, t + 100);
l += 100 + padding;
}

}

}


Activity的XML布局:

<relativelayout 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" android:padding="10dp">

<com.example.layout.myviewgroup android:layout_width="match_parent" android:layout_height="100dp" android:background="#0000ff">

<view android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000">
<view android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00">

</view></view></com.example.layout.myviewgroup>

</relativelayout>


在自定义View中,onLayout配合onMeasure方法一起使用,可以实现自定义View的复杂布局。自定义View首先调用onMeasure进行测量,然后调用onLayout方法,动态获取子View和子View的测量大小,然后进行layout布局。

PS:最关心的 只有两点 一个是参数,一个是在哪里可以重载这个函数(自定义View里面) 关于是用layout(),还是用super.onLayout()方法
具体看需求了,

下面是RecyclerView 自定义视图里面用到的一个onLayout()方法:

public class MyRecyclerView extends RecyclerView implements OnScrollListener
{

/**
*记录当前的第一个View
*/
private View mCurrentView;
/**
* 滚动监听器
*/
private OnItemScrollChangeListener mItemScrollChangeListener;

/**
* 设置监听器
* @param mItemScrollChangeListener
*/
public void setOnItemScrollChangeListener(OnItemScrollChangeListener mItemScrollChangeListener)
{
this.mItemScrollChangeListener = mItemScrollChangeListener;
}

/**
* 自定义了一个接口方法,用于ScrollListener 每个item 滚动的点击响应事件
*/
public interface OnItemScrollChangeListener
{
void onChange(View view, int position);
}

/**
* 继承 RecyclerView 必须要重写的方法
* 构造函数,在这里设置 调用 ScrollListener的滚动事件
*/
public MyRecyclerView(Context context, AttributeSet attrs)
{
super(context, attrs);
//implements OnScrollListener 启动
this.setOnScrollListener(this);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{//左上角的left、top以及右下角right、bottom值
super.onLayout(changed, l, t, r, b);
//重载该函数,实现的功能就下面实时监听当前的子View
// 首先申明 这里是 水平混动
//需要实现的效果就是每次你滑动停止之后,srcollView 第一个View 展现在上面的大图片中
mCurrentView = getChildAt(0);

if (mItemScrollChangeListener != null)
{
mItemScrollChangeListener.onChange(mCurrentView,getChildPosition(mCurrentView));
}
}

/**
* implements OnScrollListener 必须要重载的方法
*/
@Override
public void onScrollStateChanged(int arg0)
{
}

/**
* implements OnScrollListener 必须要重载的方法
*滚动时,判断当前第一个View是否发生变化,发生才回调
*/
@Override
public void onScrolled(int arg0, int arg1)
{
View newView = getChildAt(0);

if (mItemScrollChangeListener != null)
{
if (newView != null && newView != mCurrentView)
{
mCurrentView = newView ;
mItemScrollChangeListener.onChange(mCurrentView,getChildPosition(mCurrentView));
}
}

}

}
只是拿其中一部分代码作为展示,

这是效果图:在任何一次的滑动的过程中,都是选择左边第一个作为上面大图片的展示,需要用到onLayout()方法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: