Android相对布局定位

时间:2014-01-23 15:54:51

标签: android android-layout position positioning relativelayout

我有这段代码,

    DisplayMetrics display = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(display);
    _screenHeight = display.heightPixels;

    ...

    int eFieldTopMargin = (int)(_screenHeight*0.25);
    int pFieldTopMargin = (int)(_screenHeight*0.25+90);
    int signInButtonTopMargin = (int)(_screenHeight*0.25+180);

    RelativeLayout.LayoutParams lParamsForEField = (LayoutParams) eField.getLayoutParams();
    RelativeLayout.LayoutParams lParamsForPField = (LayoutParams) pField.getLayoutParams();
    RelativeLayout.LayoutParams lParamsForSignInButton = (LayoutParams) signInButton.getLayoutParams();

    lParamsForEField.setMargins(0, eFieldTopMargin, 0, 0);
    eField.setLayoutParams(lParamsForEField);

    lParamsForPField.setMargins(0, pFieldTopMargin, 0, 0);
    pField.setLayoutParams(lParamsForPField);

    lParamsForSignInButton.setMargins(0, signInButtonTopMargin, 0, 0);
    signInButton.setLayoutParams(lParamsForSignInButton);

有人能告诉我为什么会这样吗?按钮位于文本字段上方,但其上边距更大。 Why button is above text fields, though its top margin is more?

1 个答案:

答案 0 :(得分:1)

添加此行

lParamsForSignInButton(ALIGN_PARENT_TOP);

之前

signInButton.setLayoutParams(lParamsForSignInButton);