Android的 - 如何编程方式指定重量为RelativeLayout的? [英] Android - How do I specify weight programmatically for a RelativeLayout?

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

问题描述

我想这样做编程方式 -

I want to do something like this programmatically -

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

这并不让我做一个setWeight编程。但是,我看到RelativeLayout的有一个机器人:在XML 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?

推荐答案

当您添加RelativeLayout的使用addView的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);

参考<一href="http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html">here

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

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