简体   繁体   English

在代码而不是xml问题中设置LinearLayout权重

[英]Set LinearLayout weight in code rather than xml problem

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的权重设置为0.7,它说需要3个参数,但在我的代码中却不允许我这样做:

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. 因此,这里它既设置了高度,又设置了宽度,但是文档中说的最后一个参数是重量,它不允许我放在其中。

Try this for the layoutparams for linearlayout: 尝试使用linearlayout的layoutparams:

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 :) 检查导入并将它们与您自己的:)进行比较

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


    }

}

Use LinearLayout.LayoutParams 使用LinearLayout.LayoutParams

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

The last parameter takes a float. 最后一个参数采用浮点数。 0.7 is a double, use 0.7f instead to get a float. 0.7是双0.7f ,请改用0.7f获得浮点数。

linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                        LayoutParams.WRAP_CONTENT, 
                                        0.7f));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码创建线性布局(无XML)并为元素设置权重 - Code creation linearlayout (without XML) and setting weight to elements 从代码设置宽度/高度百分比或使用LinearLayout和重量? - Set width/height percentage from code or use LinearLayout and weight? 以编程方式设置<error-page>使用代码而不是 web.xml - Programmatically set <error-page> using code rather than web.xml 无法在LinearLayout和Weight属性集中调整图像大小 - Unable to resize images in LinearLayout and Weight property set 骆驼交换主体设置为xml而不是POJO对象 - Camel Exchange body set as xml rather than POJO Object 如何使用 java 代码而不是 xml 代码将按钮添加到 android 工作室中的另一个活动 - How do I add button to another activity in android studio using java code rather than xml code Android:线性布局权重 - Android: LinearLayout Weight 有什么方法可以在tomcat conf中设置可分发元素,而不是WEB-INF / web.xml? - Any way to set distributable element in tomcat conf rather than WEB-INF/web.xml? Android:使用XML布局列表单元格而不是Java代码布局(小部件) - Android: Use XML Layout for List Cell rather than Java Code Layout (Widgets) 将XML资源存储为对象而不是文件 - Storing an XML resource as an object rather than a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM