首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

RelativeLayout.LayoutParams学习与运用

其实LayoutParams就是代表一个布局属性,每一个ViewGroup对应一种LayoutParams。LinearLayout对应LinearLayout.LayoutParams,RelativeLayout对应RelativeLayout.LayoutParams。我们在XML中写的大多数属性,在代码中通过LayoutParams同样可以操作界面布局。下面以RelativeLayout.LayoutParams为例: (1)RelativeLayout.LayoutParams.setMargins(left, top, right, bottom) 通过此方法可以动态设置margin值; (2)addRule(int verb) 该方法表示所设置节点的属性不能与其他兄弟节点相关或者属性值为布尔值。 比如 addRule(RelativeLayout.CENTER_VERTICAL)就表示在RelativeLayout中的相应节点是垂直居中的。 (3)addRule(int verb,int anchor)该方法表示所设置节点的属性必须关联其他兄弟节点或者属性值为布尔值。 比如addRule(RelativeLayout.ALIGN_LEFT,R.id.date)就表示RelativeLayout中的相应节点放置在一个id值为date的兄弟节点的左边。

01

解决异常Circular dependencies cannot exist in RelativeLayout

今天碰到这个error:E/AndroidRuntime( 4657): Uncaught handler: thread main exiting due to uncaught e xception E/AndroidRuntime( 4657): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout 有点郁闷,我用的是skd1.5,在1.5的机器上(HTC G3)已经测试过了,没有问题的,但放在华为c8500(2.1update)上就报上面的错了,怎么回事呢? 根据提示判断应该是布局的原因,于是找到RelativeLayout的布局,找出最可疑的那个,注释后,不报错了。好就是他的原因,挨个看里面的元素,看属性,没错啊,后来发现, <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"> <TextView android:id="@+id/titleName" android:text="首页" android:textColor="@color/white" android:layout_toLeftOf="@+id/homeBtn" android:layout_marginRight="2px" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ImageButton android:id="@+id/homeBtn" android:layout_toRightOf="@+id/titleName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/main" android:background="@null" android:layout_marginRight="10px"> </ImageButton> </RelativeLayout> 后来改成: <RelativeLayout android:layout_width="wrap_content" android:layout_marginRight="10px" android:layout_height="wrap_content" android:layout_alignParentRight="true"> <TextView android:id="@+id/titleName" android:text="首页" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ImageButton android:id="@+id/homeBtn" android:layout_marginLeft="2px" android:layout_marginTop="2px" android:layout_toRightOf="@+id/titleName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/main" android:background="@null" > </ImageButton> </RelativeLayout> 能看到区别吗?对就是在titleName中去掉了相对homeBtn的位置信息。再看看报错提示,人家说我在RelativeLayout中存在循环的相关,就是说的这个了。 不过1.5版本的不报错,这就是后续版本的改进吗?

02

Android开发笔记(一百八十七)利用估值器实现弹幕动画

如今上网看电影电视越发流行了,追剧的时候经常看到视频上方数行评论向左边飘去,犹如子弹那样飞快掠过,这些评论文字因此得名“弹幕”。弹幕评论由正在观看的网友们即兴发表,故而连绵不绝从画面右端不断涌现,直到漂至画面左端才隐没消失。 虽然弹幕效果可使用平移动画实现,但平移动画比较单调,只能控制位移,不能控制速率、文字大小、文字颜色等要素。若想同时操纵视图的多种属性要素,需要采用属性动画加以实现。 然而视图的位移大小由间距属性margin控制,该属性又分为上下左右四个方向,更要命的是,这几个margin并非视图View类的属性,而是布局参数LayoutParams的属性,意味着无法通过margin***直接构造属性动画对象。为了动态调整margin这种非常规属性,就要引入估值器实时计算当前的属性值,再据此设置自定义控件的状态参数。 以间距属性为例,它的动画步骤说明如下: 1、定义一个间距估值器,它实现了接口TypeEvaluator的evaluate方法,并在该方法中返回指定时间点的间距数值; 2、调用ValueAnimator类的ofObject方法,根据间距估值器、开始位置和结束位置构建属性动画对象; 3、调用属性动画对象的addUpdateListener方法设置刷新监听器,在监听器内部获取当前的间距数值,并调整视图此时的布局参数; 具体到编码实现上,需要自定义弹幕视图,其内部在垂直方向排列,每行放置一个相对布局。发表弹幕评论时,先随机挑选某行相对布局,在该布局右侧添加文本视图,再通过前述的间距动画向左渐次滑动。弹幕视图的定义代码示例如下:

02

Android开发笔记(三十五)页面布局视图

布局视图有五类,分别是线性布局LinearLayout、相对布局RelativeLayout、框架布局FrameLayout、绝对布局AbsoluteLayout、表格布局TableLayout。其中最常用的是LinearLayout,它适用于包括简单布局在内的多数情况;其次常用的是RelativeLayout,它适用于一些复杂布局,主要是对相对位置要求较多的情况;再次就是FrameLayout,它一般用于需要叠加展示的场合,比如说给整个页面设置一个背景布局等等。AbsoluteLayout和TableLayout实际中很少用,基本不用关心。 另外还有纵向滚动视图ScrollView,以及横向滚动视图HorizontalScrollView,其作用顾名思义便是让它们的子视图可以在某个方向上滚动罢了。

03
领券