android的layout_android可拖拽layout

android的layout_android可拖拽layoutViewGroup.LayoutParams介绍LayoutParams携带了子控件针对父控件的信息,告诉父控件如何放置自己LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:1,一个确定的值;2,FILL_PARENT,即填满(和父容器一样大小);3,WRAP_CONTENT,即包裹住组件就好。每一个ViewGroup(例如LinearLayout,RelativeLa…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

ViewGroup.LayoutParams介绍

LayoutParams携带了子控件针对父控件的信息,告诉父控件如何放置自己

LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:

1,一个确定的值;

2,FILL_PARENT,即填满(和父容器一样大小);

3,WRAP_CONTENT,即包裹住组件就好。

每一个ViewGroup(例如LinearLayout, RelativeLayout, CoordinatorLayout, etc)需要存储有关其孩子view的属性信息。它的孩子view被放在ViewGroup,这些位置信息存储在一个包装类viewgroup.layoutparams对象中。

为了包含一个特定的布局的具体参数,viewgroup使用layoutparams Viewgroup类的子类来存储。

例如

linearlayout.layoutparams

relativelayout.layoutparams

coordinatorlayout.layoutparams

对于margin有一个ViewGroup.MarginLayoutParams类代替ViewGroup.LayoutParams。

获取ViewGroup.LayoutParams

getLayoutParams()方法可以获取ViewGroup.LayoutParams对象。

举例如下

public class ExampleView extends View {

public ExampleView(Context context) {

super(context);

setupView(context);

}

public ExampleView(Context context, AttributeSet attrs) {

super(context, attrs);

setupView(context);

}

public ExampleView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

setupView(context);

}

private void setupView(Context context) {

if (getLayoutParams().height == 50){ // DO NOT DO THIS!

// This might produce NullPointerException

doSomething();

}

}

//…

}

ViewGroup.LayoutParams上下转型

注意LayoutParams携带了子控件针对父控件的信息,告诉父控件如何放置自己,所以要使用相应父控件的LayoutParams。

举例说明,一个LinearLayout包含FrameLayout

错误使用

FrameLayout innerLayout = (FrameLayout)findViewById(R.id.inner_layout);

FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) innerLayout.getLayoutParams();

正确的使用

FrameLayout innerLayout = (FrameLayout)findViewById(R.id.inner_layout);

LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) innerLayout.getLayoutParams();

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191022.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号