简体   繁体   中英

Adjusting height of linear layout programmatically

Whenever I try setting the height of a LinearLayout, I always get this exception:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

Here is my code:

LinearLayout.LayoutParams  hide = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
LinearLayout.LayoutParams  show = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);

driverMainOptions.setLayoutParams(hide);
mapDirections.setLayoutParams(show);

Is there a specific import statement I need for this to execute properly?

This should work

LinearLayout lLayout = new LineaLayout(context);
LayoutParams params = lLayout.getLayoutParams();

params.height = 200;
params.width = 200;
lLayout.setLayoutParams(params);

see the accepted answer at Android: How to Programmatically set the size of a Layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM