Android: How to Programmatically set the size of a Layout

前端 未结 4 561
南方客
南方客 2020-11-28 01:09

As part of an Android App I am building a button set. The buttons are part of a nested set of LinearLayouts. Using weight I have the set resizing itself automatically based

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 01:46

    Java

    This should work:

    // Gets linearlayout
    LinearLayout layout = 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);
    

    If you want to convert dip to pixels, use this:

    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, , getResources().getDisplayMetrics());
    

    Kotlin

提交回复
热议问题