如何从代码中动态设置layout_weight属性?

时间:2011-01-09 18:55:01

标签: android android-layout android-layout-weight

如何从java代码中动态设置android中按钮的属性layout_weight的值?

10 个答案:

答案 0 :(得分:405)

您可以将其作为LinearLayout.LayoutParams构造函数的一部分传递:

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT,
    1.0f
);
YOUR_VIEW.setLayoutParams(param);

最后一个参数是重量。

答案 1 :(得分:111)

使用LinearLayout.LayoutParams

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
编辑:啊,Erich的答案更容易!

答案 2 :(得分:78)

如果您已经在layout(xml)文件中定义了视图,只想以编程方式更改权重,这种方式更好

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)   
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);

new a LayoutParams会覆盖你的xml文件中定义的其他参数,如margin,或者你需要在LayoutParams中指定所有这些参数。

答案 3 :(得分:20)

如果宽度,高度和重量的构造函数不起作用,请尝试使用宽度和高度的构造函数。然后手动设置重量。

如果要根据权重设置宽度,请在构造函数中将width设置为0。同样适用于身高。 下面的代码适用于我。

LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);

LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam2.weight = 0.7f;
child2.setLayoutParams(childParam2);

parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);

答案 4 :(得分:14)

如果我有人在寻找答案,请使用:

LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;

如果要从xml文件初始化布局,这比为线性布局提供新的布局参数要方便得多。

答案 5 :(得分:6)

LinearLayout.LayoutParamsTableLayout.LayoutParams中的任何一个为我工作,对于正确的按钮TableRow.LayoutParams。那就是:

            TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1f);

关于使用MATCH_PARENTWRAP_CONTENT是否相同。

答案 6 :(得分:3)

如果已经在布局(xml)文件中定义了视图,并且只想以编程方式更改权重,那么创建新的LayoutParams将覆盖xml文件中定义的其他参数。

首先你应该使用“getLayoutParams”然后使用setLayoutParams

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams(); params.weight = 4f; mButton.setLayoutParams(params);

答案 7 :(得分:1)

如果已定义 #include <iostream> #include <cmath> #include <string> using namespace std; //Define a function that will decide if someone can rent an apartment based on the salary he/she makes. //The rent should be at most one-third of the salary. If it is more than one third, the customer’s application will be denied. bool canbuy(double salary, double rent); const double rent = 1200; int main() { int salary; int age; char ans; string fullname, lastname; do { cout << "Hello. Welcome to the Renting Evolution Center... " << endl << endl; cout << "This apartment's monthly rent is $1200. " << endl << endl; cout << "I need to gather your information to see if you qualify: " << endl << endl; cout << "Your full name, please: "; cin >> fullname; cin >> lastname; cout << endl; cout << "Your age, please: "; cin >> age; cout << endl; cout << "Your salary, please: "; cin >> salary; cout << endl; cout << "We will now process your information to see if you are qualified to purchase a house..." << endl << endl; system("pause"); if (age > 17) { if (canbuy(salary, rent)) { cout << "Based on the information you have provided, you are qualified. You are old enough and have money." << endl << endl; } else { cout << "Based on the information you have provided, you are not qualified. Sorry... Make more money next time. " << endl << endl; } } else { cout << "Based on the information you have provided, you are not qualified. Sorry... Get older next time..." << endl << endl; } cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "Would you like to enter new information?: "; cin >> ans; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << endl; } while (ans == 'y' || ans == 'Y'); cout << "Goodluck with house shopping! " << endl; system("pause"); return 0; } bool canbuy(double salary, double rent) { if (rent <= ((1 / 3)* salary)) { return true; } else { return false; } return canbuy; } (以XML格式或动态定义),则以下是单行内容:

layoutparams

答案 8 :(得分:0)

使用Kotlin你可以做类似的事情:

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.widget.*

import android.widget.LinearLayout

class RespondTo : CardView {
    constructor(context: Context) : super(context) {
        init(context)
    }

    private fun init(context: Context) {


        val parent = LinearLayout(context)

        parent.apply {
            layoutParams = LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT, 1.0f).apply {
                orientation = LinearLayout.HORIZONTAL

                addView(EditText(context).apply {
                    id = generateViewId()
                    layoutParams = LinearLayout.LayoutParams(0,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 0.9f).apply {
                    }
                })
                addView(ImageButton(context).apply({
                    layoutParams = LinearLayout.LayoutParams(0,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 0.1f)
                    background = null
                    setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
                    id = generateViewId()
                    layoutParams = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT).apply {
                        addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
                        // addRule(RelativeLayout.LEFT_OF, myImageButton.id)
                    }
                }))
            }
        }
        this.addView(parent)
    }
}

答案 9 :(得分:0)

科尔廷

        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 2.0f)

        params.weight = 1.0f
        button.layoutParams = params