我可以设置“android:layout_below"吗?在运行时以编程方式? [英] Can I Set "android:layout_below" at Runtime Programmatically?

查看:26
本文介绍了我可以设置“android:layout_below"吗?在运行时以编程方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时创建 RelativeLayout 以编程方式设置 android:layout_below 的等效项?

Is it possible when creating a RelativeLayout at runtime to set the equivalent of android:layout_below programmatically?

推荐答案

是:

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.BELOW, R.id.below_id);
viewToLayout.setLayoutParams(params);

首先,代码通过指定高度和宽度来创建一个新的布局参数.addRule 方法在 android:layout_below 中添加了 xml 的等效项.然后你只需在你想要拥有这些参数的视图上调用 View#setLayoutParams .

First, the code creates a new layout params by specifying the height and width. The addRule method adds the equivalent of the xml properly android:layout_below. Then you just call View#setLayoutParams on the view you want to have those params.

这篇关于我可以设置“android:layout_below"吗?在运行时以编程方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆