android getLayoutParams介绍和区别

自己的笔记,对于Layoutparams.getLayoutParams
转载于他人……(好无耻啊…)

  • android.view..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.
Returns:The LayoutParams associated with this view
这个类是用来取得它所有的父布局中的一个参数,如果一个布局中我们只写一个ImageView,我们调用imageView.getLayoutParams()根本就不能取得数据,因为它需要一个父布局来提供这个参数。

方式一:

LinearLayout layout=new LinearLayout(context);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ImageView imageView=new ImageView(context);
params.width=200;
params.height=200;
imageView.setBackgroundResource(R.drawable.icon);
layout.addView(imageView,params);
return layout;

方式二:

LinearLayout layout=new LinearLayout(context);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ImageView imageView=new ImageView(context);
imageView.setBackgroundResource(R.drawable.icon);
layout.addView(imageView,params);
LayoutParams p=imageView.getLayoutParams();
p.width=200;
p.height=200;
imageView.setLayoutParams(p); (这里需要再次声明一下,不设回去的话也是可以的,前面做实验没有注意,不设也可以达到效果)
return layout;

他居然return返回,好吧这两种返回都可以实现getLayoutParams的值得获取,这个方法主要获取子控件在父控件的布局就是宽高了…

注意:
1.方式一和方式二的结果是一样的,我们可以在未加入imageView前设置params.height和width来控制imgeView的大小,我们也可以在加入了ImageView之后用imageView.getLayoutParams()来获取布局参数,二次设置布局参数,

2.如果我们的imageView没有一个Layout去装载它,调用它的getLayoutParams()是肯定不会取得值的,会报空指针异常,因为这个方法是取得它布局的参数,就像我们人说我们今天穿的一件蓝色的衣服,如果我们都没有穿衣服,何来说我们今天穿的什么呢?所以要注意。
3.imageView.setLayuoutParams是二次从布局中获取值后再次设置回去,这个一定要注意,这里以前曾疑惑。

分析一例:
我们手动新建一个LinerLayout,我们在加入我们的imageView的时候,我们需要新建一个LinerLayout.LayoutParams,定义我们加入ImageView的一个规则,在我们加入之后,我们可以设置它的高度,如我我们用到了params.width=200….,于是我们在加入的时候,我们的LinerLayout就会自动地把它的宽高压缩成200 ,于是出来我们想要的结果。

分析二例:
在我们向LinerLayout加入我们的imageView后,我们还可以修改加入ImageView的一个放入规则,这个规则就是LayoutParams,它怎么获得呢,它需要用layout.getLayoutParams()来获得。
关键,如果一个ImageView外面没有一个Layout包裹,它是不会得到布局的LayoutParams的,因为它根据没有,得到以后,我们设置了p的宽和高,我们还必须要设置回去才能生效,这一定要注意。

这个LayoutParams.getLayoutParams是针对在父控件中的View参数获取
这个LayoutParams.getLayoutParams是针对在父控件中的View参数获取
这个LayoutParams.getLayoutParams是针对在父控件中的View参数获取

重要的事情说三遍…

其实有其他方法获取View的参数getview.getMeasuredHeight()重点内容
有人问他们之间的区别,于是我抄过来了,又无耻了一次…

LayoutParams leftChildLayoutParams = mLeftChild.getLayoutParams();//获得子view向父容器申请的属性,就是前面带layout的属性

int leftChildHeightMeasureSpec = MeasureSpec
.makeMeasureSpec(leftChildLayoutParams.height, MeasureSpec.EXACTLY);

mLeftChild.measure(widthMeasureSpec, leftChildHeightMeasureSpec);//调用这里后才有tMeasuredHeight()

getLayoutParams()是获取你在布局文件里面写的申请,getMeasuredHeight()是获得测量后的属性,如果测量的model不是MeasureSpec.EXACTLY,或者是值不等于getLayoutParams().height,这两个数值就不等.
坑应该填完了…

再来一个,getx与getRawx的区别
 public final float getX (int pointerIndex)
  Returns the X coordinate of this event for the given pointer index (use getPointerId(int) to find the pointer identifier for this index). Whole numbers are pixels; the value may have a fraction for input devices that are sub-pixel precise.
  public final float getRawX ()
  Returns the original raw X coordinate of this event. For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.
  大概意思应该是说,getRawX()获取的是屏幕上的原生(original raw)x坐标,而getX(int pointerIndex)只是说获取的是指定的触控点坐标,getX()就是获取第一个触控点的坐标。所以,getRawX()就是说,获取的是相对于屏幕左上角的x坐标,而getX()是获取相对控件左上角的x坐标。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值