动态增加android中的相对布局大小

use*_*237 0 android

我有一个相对布局,我正在显示一个页面和一些内容.当我缩放我的页面时......布局大小没有增加.我希望我的布局动态增加其大小.我该怎么设置?我尝试用java代码做.

contentLayout.getLayoutParams().height = x (some value which is equal to the page size)
contentLayout.requestLayout()
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我还设置布局PARAMS android:layout_widthandroid:layout_heightwrap-content在XML文件中.

请帮助我.谢谢

Zel*_*mir 8

你可以这样做.我还添加了边距的设置:

RelativeLayout targetItem = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(mWidth, mHeight);
adaptLayout.setMargins(marginLeft, marginTop, marginRight, marginBottom);   
targetItem.setLayoutParams(adaptLayout);                    
Run Code Online (Sandbox Code Playgroud)

对于mWidth和mHeight,您还可以设置dip值以使其适应不同的屏幕尺寸.简单的方法是在你的中定义Dimension strings.xml并使用它们,而不是使用绝对值.

所以,如果你定义relative_width为120dip,relative_height然后在你的代码中定义为100 dip

RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(getResources().getDimension(R.dimen.relative_width), getResources().getDimension(R.dimen.relative_height));
Run Code Online (Sandbox Code Playgroud)