这是我的场景。设置android的相对布局的高度和宽度
我有被制成每320 * 480分辨率(假设我使用MDPI移动)的图像。我将此图像作为背景添加到相对布局。
现在我的屏幕有三个主要的东西。
1- Title bar (The standard title bar of android)
2- My relative layout and its sub view image relative layout which is matched parent.
3- My menu bar at the bottom.
现在,因为我的图像在菜单栏和标题栏之间拉伸。我想让它适合屏幕。以下是我正在使用的代码。
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout()
{
if (notCreated == true)
{
notCreated = false;
}
else
{
mnHeight = mainLayout.getHeight();
Rect rect = getRawCoordinatesRect(mainLayout);
h = getWindowManager().getDefaultDisplay().getHeight() -
- rect.top - mainLayout.getHeight();
// rect.top to remove android standard title bar
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(image.getWidth(),LayoutParams.MATCH_PARENT);
params.topMargin = -1 * rect.top;
params.bottomMargin = -1 * h;
image.setLayoutParams(params);
}
}
});
现在这是行不通的。一点帮助,将不胜感激。
注:其实我只是想舒展我的形象相对布局到320 * 480。现在它在标题栏和我的菜单栏之间切换。 E.g现在它的尺寸为320 * 400
对不起,如果我没有正确解释它,请检查它我编辑了问题。其实我只是想将我的图像相对布局拉伸到320 * 480。现在它在标题栏和我的菜单栏之间切换。例如,现在它的尺寸是320 * 430 –
尝试阅读关于此问题的第一个答案:http://stackoverflow.com/questions/4756606/how-to-set-height-of-relative-layout-dynamically-in-android – patrickwlarsen