使用LinearLayout.LayoutParams将WRAP_CONTENT更改为设定的数字

时间:2016-09-09 09:44:14

标签: android android-linearlayout layoutparams

我已经设置了一些LinearLayout.LayoutParams来设置一些只使用java的按钮。但我想将它从WRAP_CONTENT更改为我选择的高度和宽度。

//LinearLayout.LayoutParams top;
//this is declared at the top as it is used by different bits of the code.
top = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    top.topMargin = 100;
    top.leftMargin = 120;

4 个答案:

答案 0 :(得分:2)

top = new LinearLayout.LayoutParams
            (90, 80); 

// 无论你喜欢什么

这来自文档:

public LayoutParams(int width, int height) {
        super(width, height);
        weight = 0;
    }

答案 1 :(得分:0)

根据ViewGroup.MarginLayoutParams.html#setMargins你必须使用这个:

LinearLayout layout = (LinearLayout)findViewById(R.id.yourLinear_Layout);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);            
params.setMargins(120, 100, 0, 0); //Here your custom margin (int left, int top, int right, int bottom)
layout.setLayoutParams(params);

答案 2 :(得分:0)

你试过吗?

top.getLayoutParams().height = 256;
top.getLayoutParams().width= 256;
top.requestLayout();

答案 3 :(得分:0)

试试这个..

LinearLayout layout = (LinearLayout)findViewById(R.id.numberPadLayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
layout.setLayoutParams(params);