Tag: android relativelayout

在自定义布局中定位充气的视图

我写了一个继承自RelativeLayout的自定义布局。 它有一个方法(在布局被膨胀后调用),当被调用时会膨胀一个视图,将其附加到布局,然后将其与布局相关联。 但是,当我尝试设置布局参数时,它似乎什么都不做: fun createCell() { // Inflate a cell and attach it to this val cell = inflate(context, eventCellId, this) // Setup layout params for the cell val params = RelativeLayout.LayoutParams(400, 400) // Lets force it to be 200 from the top params.topMargin = 200 // Set the cell layout params cell.layoutParams = params […]

如何以编程方式在RelativeLayout中布局视图?

我试图通过编程实现以下(而不是通过XML声明): <RelativeLayout…> <TextView … android:id="@+id/label1" /> <TextView … android:id="@+id/label2" android:layout_below: "@id/label1" /> </RelativeLayout> 换句话说,如何让第二个TextView出现在第一个TextView下面,但我想在代码中做到这一点: RelativeLayout layout = new RelativeLayout(this); TextView label1 = new TextView(this); TextView label2 = new TextView(this); … layout.addView(label1); layout.addView(label2); setContentView(layout); 更新: 谢谢,TreeUK。 我理解大方向,但仍然不起作用 – “B”重叠“A”。 我究竟做错了什么? RelativeLayout layout = new RelativeLayout(this); TextView tv1 = new TextView(this); tv1.setText("A"); TextView tv2 = new TextView(this); […]