2

アプリケーションはscrollviewXML レイアウトになっています。

<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);

なぜこうなった?

4

1 に答える 1

5

以下に示すように xml を変更します - fillViewport
を true として 追加します。

<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"
   >

相対レイアウト追加部分は不要なので削除。

于 2013-03-29T04:54:52.693 に答える