getLayoutParams()方法和setLayoutParams()方法

参考来源:http://blog.csdn.net/liuhaomatou/article/details/22899925#comments
1. getLayoutParams()和setLayoutParams()方法的解析
a. getLayoutParams():

/**
     * Get the LayoutParams associated with this view. All views should have
     * layout parameters. These supply parameters to the parent of this
     * view specifying how it should be arranged. There are many subclasses of
     * ViewGroup.LayoutParams, and these correspond to the different subclasses
     * of ViewGroup that are responsible for arranging their children.
     *
     * This method may return null if this View is not attached to a parent
     * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
     * was not invoked successfully. When a View is attached to a parent
     * ViewGroup, this method must not return null.
     *
     * @return The LayoutParams associated with this view, or null if no
     *         parameters have been set yet
     */
  public ViewGroup.LayoutParams getLayoutParams() {
    return mLayoutParams;
}

大意就是getLayoutParams()方法的调用必须要有父控件。否则返回的就是null对象。该方法返回的是该控件的LayoutParams 对象。

b. setLayoutParams():
/**
     * Set the layout parameters associated with this view. These supply
     * parameters to the parent of this view specifying how it should be
     * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
     * correspond to the different subclasses of ViewGroup that are responsible
     * for arranging their children.
     *
     * @param params The layout parameters for this view, cannot be null
     */
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        if (params == null) {
            throw new NullPointerException("Layout parameters cannot be null");
        }
        mLayoutParams = params;
        resolveLayoutParams();
        if (mParent instanceof ViewGroup) {
            ((ViewGroup) mParent).onSetLayoutParams(this, params);
        }
        requestLayout();
    }

设置该控件的布局参数,也就是将修改好的参数信息重新赋值给该控件。

如下的示例代码:

 mImageViewTabline = (ImageView) findViewById(R.id.iv_tabline);
        //
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        //  获取到屏幕的的三分之一
        mScreen1_3 = displayMetrics.widthPixels / 3;
        // 获取到tabline控件的LayoutParams
        LayoutParams params = mImageViewTabline.getLayoutParams();
        // 设置tabline的宽度
        params.width = mScreen1_3;
        // 将修改好的的宽度设置给tabline控件
        mImageViewTabline.setLayoutParams(params);
  1. 关于setLayoutParams报错
      在继承BaseAdapter的时候,用getView返回View的时候,用代码控制布局,需要用到View.setLayoutParams,但是报错了,报的是类型转换错误,经过研究,发现,这里不能使用ViewGroup.LayoutParams而必须使用对应父View的LayoutParams类型。如:某View被LinearLayout包含,则该View的setLayoutParams参数类型必须是LinearLayout.LayoutParams。原因在于LinearLayout(或其他继承自ViewGroup的layout,如:RelativeLayout)在进行递归布局的时候,LinearLayout会获取子View的LayoutParams,并强制转换成LinearLayout.LayoutParams,如LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();

或者是如下定义:
LayoutParams lp = (LayoutParams) child.getLayoutParams();

以转换成内部类型LinearLayout.LayoutParams。

自己测试运行的时候报空指针,原因为child.getLayoutParams();这里没有获得到子控件所在的布局,查看代码发现parent.addView(child);应该写在上面,之后child才能getLayoutParams();

  1. LayoutParams类的解析
    LayoutParams继承于Android.View.ViewGroup.LayoutParams.
    LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
    可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。
    但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
    1,一个确定的值;
    2,FILL_PARENT,即填满(和父容器一样大小);
    3,WRAP_CONTENT,即包裹住组件就好。
    LayoutParams类是用于child view(子视图) 向 parent view(父视图)传达自己的意愿的一个东西(孩子想变成什么样向其父亲说明)其实子视图父视图可以简单理解成一个LinearLayout 和 这个LinearLayout里边一个 TextView 的关系 。TextView 就是LinearLayout的子视图 child view 。需要注意的是LayoutParams只是ViewGroup的一个内部类,这里边这个也就是ViewGroup里边这个LayoutParams类是 base class 基类实际上每个不同的ViewGroup都有自己的LayoutParams子类。
    也就是说在LinearLayout类中有LayoutParams子类,在ViewGroup里面同样有LayoutParams子类。感兴趣的可以去翻看源码就知道了。

示例代码:

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("tag", position + "," + positionOffset + "," + positionOffsetPixels);
                // 获取到父控件的参数
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mImageViewTabline.getLayoutParams();
                // 0--->1
                if (mCurrentPageIndex == 0 && position == 0) {
                    layoutParams.leftMargin = (int) (positionOffset * mScreen1_3 + mCurrentPageIndex * mScreen1_3);
                } else if (mCurrentPageIndex == 1 && position == 0) {
                    //1---->0
                    layoutParams.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1) * mScreen1_3);
                } else if (mCurrentPageIndex == 1 && position == 2) {
                    // 1--->2
                    layoutParams.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + positionOffset * mScreen1_3);
                } else if (mCurrentPageIndex == 2 && position == 1) {
                    // 2--->1
                    layoutParams.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1) * mScreen1_3);
                }

                // 设置宽度给tabline
                mImageViewTabline.setLayoutParams(layoutParams);

            }

你可能感兴趣的:(android基础)