Android:RelativeLayout.LayoutParams的位置不起作用?

时间:2013-11-07 19:01:04

标签: android

我使用Android Studio创建了新的应用程序并更改了onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.activity_main, new PlaceholderFragment())
                .commit();
    }

    int width = 400;
    int height = 20;
    RelativeLayout.LayoutParams params;
    params = new RelativeLayout.LayoutParams(
            width,
            height
    );
    params.leftMargin = 20;
    params.topMargin = 200;

    TextView textView = new TextView(this);
    textView.setText("Text Added dinamically");
    textView.setLayoutParams(params);

    FrameLayout activityMain = (FrameLayout) findViewById(R.id.activity_main);
    activityMain.addView(textView);

}

我预计会在距离顶部200个距离处看到“文字添加动态”。事实上它似乎挺举顶部。我该如何解决这个问题?

0 个答案:

没有答案