设置的LinearLayout重量code,而不是XML问题 [英] Set LinearLayout weight in code rather than xml problem

查看:164
本文介绍了设置的LinearLayout重量code,而不是XML问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code尝试和的LinearLayout的重量联机设定为0.7,看着文档后的一些例子它说,它需要3个参数,但在我的code它不会让我

I have the following code to try and set the weight of a linearLayout to 0.7, after looking at the documentation and some examples online it says it takes 3 parameters but in my code it will not let me:

LinearLayout linear = new LinearLayout(this); 
linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

所以这里同时设置的高度和宽度,但它在文档中表示,最后一个参数是重量,它不会让我把它放在那里。

So here it sets both the height and the width but the last parameter it says in the documentation is the weight and it wont let me put it in there.

推荐答案

试试这个对的LayoutParams为的LinearLayout:

Try this for the layoutparams for linearlayout:

linear.setLayoutParams(
        new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,   
                                      LayoutParams.WRAP_CONTENT,
                                      1)
);

作为证明的概念:这个作品。检查出的进口,并与自己的比较:)

As a proof-of-concept: this works. check out the import and compare them with your own :)

文件:Test.java

file: Test.java

package com.huiges.stackOverFlow.examples

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class Test extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        LinearLayout linear = new LinearLayout(this); 
        linear.setLayoutParams(
                new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,   
                                              LayoutParams.WRAP_CONTENT,
                                              1)
        );


    }

}

这篇关于设置的LinearLayout重量code,而不是XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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