Android布局使用其他按钮查看页面中的可滚动文本

时间:2012-06-06 08:45:47

标签: android android-layout

我尝试使用可滚动文本制作页面。我使用相对布局线性布局和滚动。但不知何故,这些文本不是从开头画出来的。

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

这是我的代码:

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

    }   

}

1 个答案:

答案 0 :(得分:1)

仅针对 lin2 layout_gravitycenter_horizontal更改为LinearLayout

android:layout_gravity="center_horizontal"

我不明白为什么当父center_vertical高度设置为ScrollView时你也会放wrap_content