简体   繁体   English

在运行时调整android线性布局的大小无法正常工作

[英]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. 我在相对布局中有线性布局,xml文件中线性布局的默认宽度和高度为250和150,看起来不错。 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. 但是,当我在运行时调整大小并再次将width和height设置为250和150时,它看起来并不像以前那样正确,并且看起来宽度和高度在视觉上比以前更小。 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 . 我认为我们在运行时设置的值是以像素为单位,而不是以dip单位,但我不确定。 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: 尝试使用DP代替,下面是一种转换为DP的方法:

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

Switch out YOURVALUE for a value in PX, such as 250 切换出YOURVALUE以获取PX中的值,例如250

Try like this: 尝试这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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