Dynamically changing the linearlayout width or height on Android

前端 未结 4 1368
离开以前
离开以前 2020-12-08 10:28

I am trying to change linear layout or any other widget width or height dynamically but throwing exception.

My layout is:



        
4条回答
  •  旧巷少年郎
    2020-12-08 11:00

    You can use this method inside your utils class to set height and width dynamically.

     public void setWidthAndHeight(Context context,RelativeLayout view,int width, int height) {
                    width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, context.getResources().getDisplayMetrics());//used to convert you width integer value same as dp
                    height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, context.getResources().getDisplayMetrics());
                   //note : if your layout is LinearLayout then use LinearLayout.LayoutParam
                    view.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
             }
    

提交回复
热议问题