简体   繁体   中英

resizeing android linear layout at runtime not work right

i have a linear layout within a relative layout , default width and height of the linear layout in xml file is 250 and 150 and it looks good. but when i resize it at runtime and again set width and height to 250 and 150 its not look right like before and it seems width and height visually less than before. i use this code to resize at runtime :

    LinearLayout ll = (LinearLayout)findViewById(R.id.myLayout);
    ll.getLayoutParams().height=150;
    ll.getLayoutParams().width=250;
    ll.requestLayout();
    ll.invalidate();

i think the values we set at runtime are in pixel unit not in dip unit , but im not sure . if it is true , how can i change the unit of width and height at runtime ?

Try it with DP instead, below is a way too convert to DP:

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

Switch out YOURVALUE for a value in PX, such as 250

Try like this:

Layoutparams params = ll.getlayoutparams();params.height=300;params.width= 200;ll.setlayoutparams(params);

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