通过android java对齐textview

通过android java对齐textview,java,android,Java,Android,目前,一切都是通过java代码完成的。我正在创建一个相对布局,然后向其中添加我的GL曲面视图和一些文本视图。问题是我无法将一个文本视图与左上角对齐,另一个与右上角对齐。我的代码有什么问题。还有一种更有效的方法,比如通过XML来实现。因为我试过了,但没用 r1 = new RelativeLayout(this); view = new MyGLSurfaceView(this); view.setKeepScreenOn(true);//keeps activity fr

目前,一切都是通过java代码完成的。我正在创建一个相对布局,然后向其中添加我的GL曲面视图和一些文本视图。问题是我无法将一个文本视图与左上角对齐,另一个与右上角对齐。我的代码有什么问题。还有一种更有效的方法,比如通过XML来实现。因为我试过了,但没用

    r1 = new RelativeLayout(this);
    view = new MyGLSurfaceView(this);
    view.setKeepScreenOn(true);//keeps activity from going to sleep

    r1.addView(view);
    TextView text1 = new TextView(this);
    TextView text2 = new TextView(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
    RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200);
    lp.addRule(RelativeLayout.ALIGN_RIGHT);
    text1.setLayoutParams(lp);
    text1.setText("3 Star");
    text1.setBackgroundColor(0x4060ff70);
    rp.addRule(RelativeLayout.ALIGN_LEFT);
    text2.setLayoutParams(rp);
    text2.setText("4 Star");
    text1.setTextSize(30);
    text2.setTextSize(30);
    //text2.setBackgroundColor(0x4060ff70);
    r1.addView(text1);
    r1.addView(text2);

    setContentView(r1);

这可以在XML中完成,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Left" />

    <TextView
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right" />
</RelativeLayout>


java代码怎么样。我应该如何更改它以使其引用此xml文件将xml文件放在res/layout中。然后
setContentView(R.layout.filename)