首页 > 其他分享 >Android LayoutParam,MarginLayoutParams

Android LayoutParam,MarginLayoutParams

时间:2022-11-04 12:06:01浏览次数:67  
标签:ViewGroup 设置 LayoutParams 子类 params LayoutParam MarginLayoutParams Android View


前言


开发中经常会遇到一个场景,给View动态设置 margin 边距,针对容器类布局比较直观。对非容器类进行 margin 边距设置需按不同的LayoutParams设置,否则很容造成异常。

问题:

为什么给父布局(RelativeLayout)下的子 View(ImamgeView) 设置 LinearLayout.LayoutParams ,提示类型转换异常?

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot 
be cast to android.widget.RelativeLayout$LayoutParams

LayoutParams 的子类:

ViewGroup.MarginLayoutParams
FrameLayout.LayoutParams
LinearLayout.LayoutParams
RelativeLayout.LayoutParams
RecyclerView.LayoutParams
GridLayoutManager.LayoutParams
StaggeredGridLayoutManager.LayoutParams
ViewPager.LayoutParams
WindowManager.LayoutParams

设置ImageView的 LayoutParams 需要从 LayoutParams 的子类选择一个。查看api 未发现针对View的 LayoutParams. 如下这种格式:

ImageView.LayoutParams
TextView.LayoutParams

看到的是 容器类.LayoutParams 最终继承 ViewGroup.LayoutParams

继承关系


xxxLayout.LayoutParams -> ViewGroup.MarginLayoutParams-> ViewGroup.LayoutParams

view类中方法setLayoutParams()入参要求必须是 ViewGroup.LayoutParams及子类。
/**
* Set the layout parameters associated with this view. These supply
* parameters to the <i>parent</i> 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();
}

大概意思是说:设置与此视图关联的布局参数。这些为该视图的 parent 布局提供参数, 告诉父布局应如何排列子布局。 ViewGroup.LayoutParams 有很多子类,它们对应着不同的 ViewGroup 子类,它们负责安排他们的子类。重要的一点是,子View必须告诉parentView如何对子View进行布局,如果添加子View的时候未指定LayoutParams , 默认会给子View创建 LayoutParams 对象。

marginHorizontal,marginVertical


2. ViewGroup中MarginLayoutParams 静态类中有包含两个属性 marginHorizontal,marginVertical。


marginHorizontal,设置左右margin,marginVertical设置垂直margin。优先级高于 leftMargin,rightMargin,topMargin,

引用地址
​Android LayoutParams详解​​


标签:ViewGroup,设置,LayoutParams,子类,params,LayoutParam,MarginLayoutParams,Android,View
From: https://blog.51cto.com/u_15861646/5823431

相关文章

  • Android 使用 unity 导出obb包
    1.通过unity导出包含obb的工程。2.按照google官方给定的obb命名方式,已经存放路径进行操作​​Obb命名方式​​命名方式:[main|patch]。<扩展版本>。<程序包名称>.obbeg......
  • Android LocalBroadcastManager 使用
    前言LocalBroadcastManager简单使用。1.注册【添加IntentFilter】2.反注册3.发广播publicclassMain2ActivityextendsAppCompatActivity{@Overrideprotect......
  • Android 英文数字混排导致提前换行完美解决
    前言数字加英文混排造成,段落提前换行异常。网上可找到处理方式较多。处理方式:1.自定义TextView,测量文字宽度与父窗体宽度自行进行人为换行占主流。2.全角半角进行统一,将字......
  • android studio 4.1变更
    前言这两天被androidstudio4.1升级后遇到的问题折腾的头大。虽然自己遇到的问题和网友遇到的问题不一样。总结一句话。升级需谨慎问题归问题,这次更新还是有很多亮点。官......
  • Android 基础 MaterialButton
    项目中经常会使用到,给按钮添加边框,点击效果,圆角,icon+文字圆角。发现系统就有提供好的组件,除了CardView可以设置。androidmaterialdesign支持库中各种可以直接拿来用的组......
  • android 手机 apk安装失败对应码
    下面是从网上找到的几种常见的错误及解决方法:1、INSTALL_FAILED_INVALID_APK:无效的安装包,安装包已损坏请检查安装包是否完整。如果是xpk包,可以通过手动安装xpk来检测一......
  • Android 共享内存(ashmem)持续更新
    Android共享内存(ashmem)前言项目中接入讯飞语音合成,在sdk中看到MemoryFile,了解下用法发现,看到的只是冰山一角。官方介绍:SharedMemoryenablesthecreation,mapping,and......
  • Android kotlin泛型知识点梳理
    前言学习知识需要提前设立目标,带着问题学习才能有的放矢。无论是java的泛型还是kotlin语言的泛型均是写框架,写通用工具类神器。如果不熟悉泛型语法,开发过程中将会遇到很多奇......
  • Android kotlin 类委托 by,by lazy关键
    前言接触kotlin语言也有几年时间了。日常开发工作中也推荐使用kotlin,但是对于一些kotlin语言语法的细节没有进行系统学习。碎片的知识点让工作中屡屡碰壁,前些天开始学习comp......
  • 【AGC】SDK未经用户同意获取AndroidID问题
     1.AGC-接入agc的sdk检测到未经用户同意获取AndroidId的问题。问题背景:开发者接入华为性能管理、崩溃服务、华为分析等SDK后上架小米应用商店被拒,称检测到未经用户同意获取......