Android - How do I specify weight programmatically for a RelativeLayout?

て烟熏妆下的殇ゞ 提交于 2019-12-18 03:39:12

问题


I want to do something like this programmatically -

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

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?


回答1:


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);

Reference here



来源:https://stackoverflow.com/questions/6073346/android-how-do-i-specify-weight-programmatically-for-a-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!