RelativeLayout.LayoutParams.addRule()方法

1、应用场景

在使用RelativeLayout布局的时候,通常在载入布局之前在相关的XML文件中进行静态设置即可。但是,在有些情况下,我们需要动态的设置布局 的属性,在不同条件下设置不同的布局排列方式。这个时候就需要使用到RelativeLayout.LayoutParams.addRule()方法, 该方法有两种重载方法:

(1)、addRule(int verb) 该方法表示所设置节点的属性不能与其他兄弟节点相关或者属性值为布尔值。

比如 addRule(RelativeLayout.CENTER_VERTICAL)就表示在RelativeLayout中的相应节点是垂直居中的。

(2)、addRule(int verb,int anchor)该方法表示所设置节点的属性必须关联其他兄弟节点或者属性值为布尔值。

比如addRule(RelativeLayout.ALIGN_LEFT,R.id.date)就表示RelativeLayout中的相应节点放置在一个

id值为date的兄弟节点的左边。

2、实例代码

[java] view plain copy

  1. RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.main_layout);
  2. RelativeLayout.LayoutParams lpview = new RelativeLayout.LayoutParams
  3. (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  4. lpview.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  5. lpview.addRule(RelativeLayout.ABOVE, R.id.text);
  6. GLSurfaceView glView = new GLSurfaceView(this);
  7. mainLayout.addView(glView,lpview);
时间: 2024-12-08 18:51:38

RelativeLayout.LayoutParams.addRule()方法的相关文章

Android 动态改变布局属性RelativeLayout.LayoutParams.addRule()

我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的布局排列方式,这时候就需要用到 RelativeLayout.LayoutParams.addRule() 方法,该方法有两种重载方式: addRule(int verb) :用此方法时,所设置节点的属性不能与其它兄弟节点相关联或者属性值为布尔值(布尔值的属性,设置时表示该属性为 true,不设置就

代码中动态改变布局属性RelativeLayout.LayoutParams.addRule()

我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的布局排列方式,这时候就需要用到 RelativeLayout.LayoutParams.addRule() 方法,该方法有两种重载方式: addRule(int verb) :用此方法时,所设置节点的属性不能与其它兄弟节点相关联或者属性值为布尔值(布尔值的属性,设置时表示该属性为 true,不设置就

RelativeLayout.addRule()方法

RelativeLayout.addRule()方法 通过LayoutParams的 addRule方法来额外的添加别的规则了,android.widget.RelativeLayout.LayoutParams.addRule(int verb, int anchor), 其中 anchor 参数指定可以是 View 的 id(“相对于谁”).RelativeLayout.TRUE(启用某种对齐方式)或者 是-1(应用于某些不需要 anchor 的 verb)[因为 RelativeLayou

RelativeLayout.LayoutParams 使用addRule失效的问题解决办法

Button btn1; btn1.setId(1001); int width;   // layout width; int btnWidth; // btn width; int btnHeight; // btn height; ……. RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(btnWidth, btnHeight); p.addRule(RelativeLayout.ALIGN_PARENT_TOP

addRule方法就要带一个view的id值

有了这个addRule方法就可以设置很多其他属性了,通过源码可知RelativeLayout是使用mRules这个数组来记录哪种属性使用到了,一共有22种属性: /** * Rule that aligns a child's right edge with another child's left edge. */ public static final int LEFT_OF = 0; /** * Rule that aligns a child's left edge with anoth

RelativeLayout.LayoutParams

通过id设置相对兄弟元素对齐. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/lyla_text_view"

为什么relativelayout.layoutParams的width为-1

源码里看下就知道了.. -1不代表宽度,代表MATCH_PARENT常量的值public static final int FILL_PARENT = -1; public static final int MATCH_PARENT = -1; public static final int WRAP_CONTENT = -2;

代码布局relativeLayout

后台布局   在ANDROID 开发中有时候我们需要在后台添加布局文件这里我们来说一下后台添加RelativeLayout文件的方式: RelativeLayout,顾名思义,就是以“相对”位置/对齐为基础的布局方式.android.widget.RelativeLayout 有个 继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例调用 RelativeLayout.addView 就可以实现“相对布局”. 首先我们

如果自定义一个可以点击之后展开/收缩的菜单组件

首先,定义布局文件,如下 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_alignParentBottom="true