本文介绍了Android - 如何以编程方式为 RelativeLayout 指定权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式做这样的事情 -

I want to do something like this programmatically -

<LinearLayout>
  <RelativeLayout1 weight = 1>
  <RelativeLayout2 weight = 3>
  <RelativeLayout3 weight = 1>
<LinearLayout>

它不允许我以编程方式执行 setWeight.但是,我确实看到 RelativeLayout 在 XML 中有一个 android:layout_weight 参数.我错过了什么吗?

It does not let me do a setWeight programmatically. However, I do see that RelativeLayout has an android:layout_weight parameter in XML. Am I missing something?

推荐答案

当您使用 addView 将 RelativeLayout 添加到 LinearLayout 时,您必须提供一个 LinearLayout.LayoutParams,如下所示:

When you add the RelativeLayout to the LinearLayout using addView you have to provide a LinearLayout.LayoutParams, something like this :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
linearLayout.addView(relativeLayout, params);

参考这里

这篇关于Android - 如何以编程方式为 RelativeLayout 指定权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 08:55
查看更多