2

我尝试制作一个带有可滚动文本的页面。我正在使用相对布局、线性布局和滚动。但不知何故,这些文本不是从一开始就画出来的。

前 3 行未绘制(Title1、Title2、Title3),因为它们位于滚动区域之外。但为什么?滚动在指定区域中的 2 个按钮可以正常工作/显示,如下所示: 我在这里设置 android:layout_below="@id/button_s" android:layout_above="@id/button_c" 滚动的边距,但仍然显示文本边缘之外。我做错了什么?为什么我在其中绘制文本的线性布局 lin2 从屏幕顶部开始?我为它设置了 android:layout_below。

这是我的代码:

主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


<TextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="@string/my_app" />

<Button
    android:id="@+id/button_s"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/tv"
    android:text="@string/search" />   

<Button
    android:id="@+id/button_c"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:text="@string/config" />

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"
    android:layout_below="@id/button_s"
    android:layout_above="@id/button_c" android:id="@+id/ScrollView">

    <LinearLayout
        android:layout_width="fill_parent"                      
        android:orientation="vertical"
        android:id="@+id/lin2" 
        android:layout_height="wrap_content"  
        android:layout_below="@id/button_s"
        android:layout_above="@id/button_c"
        android:layout_gravity="center_vertical|center_horizontal">

    </LinearLayout>
</ScrollView>


</RelativeLayout>

我的java文件:

String[] strTitles  ={"Title 1: test test test test test test test test",
                       "Title 2:  test test test test test",
                       "Title 3:  test test test test test",
                       "Title 4:  test test test test",
                       "Title 5:  test test test",
                       "Title 6:  test test test test",
                       "Title 7:  test test test test",
                       "Title 8:  test test test test test",
                       "Title 9:  test test test test test",
                       "Title 9:  test test test test test",
                       "Title 10:  test test test test",
                       "Title 11:  test test test test test",
                       "Title 12:  test test test test test",
                       "Title 13:  test test test test test",
                       "Title 14:  test test "};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout ll = (LinearLayout)findViewById(R.id.lin2);

    for (int i = 0 ; i < strTitles.length * 2 ; i++)
    {
        String strToDisplay = " ";
        if(i%2 == 1)
            strToDisplay = " ";
        else
        {
            strToDisplay = strTitles[i/2];
        }

        TextView tv = new TextView(this);
        tv.setText(strToDisplay);
        ll.addView(tv);
        tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

    }   

}
4

1 回答 1

1

仅将您的更改layout_gravitylin2center_horizontal LinearLayout

android:layout_gravity="center_horizontal"

我不明白为什么center_vertical当父ScrollView高度设置为wrap_content.

于 2012-06-06T09:24:28.657 回答