设置布局的水平垂直中心

在XML布局文件中有scrollview

<ScrollView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
       >
</ScrollView>

需要一个textView到ScrollView中心来。

我试过设置水平居中,但是垂直向上。我想都水平和垂直都居中。

LinearLayout l1 = new LinearLayout(getActivity());
            l1.setOrientation(LinearLayout.VERTICAL);
            l1.setGravity(Gravity.CENTER);
            l1.setBackgroundColor(Color.WHITE);
            TextView errorView = new TextView(getActivity());
            LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            errorView.setText("TextView");
            errorView.setTextColor(Color.BLACK);
            errorView.setLayoutParams(params);
            errorView.setGravity(Gravity.CENTER);
            l1.addView(errorView);
            scrollViewCon.addView(l1, lparams);

修改XML文件:
添加fillViewporttrue

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:fillViewport="true"
   >

删除不必要的布局。