2012-06-06 17 views
2

我是Android新手,我嘗試製作帶有可滾動文本的頁面。我正在使用相對佈局線性佈局和滾動。但不知怎的,文本從開始就不畫。Android佈局查看與其他按鈕的頁面中的可縮小文本

前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

更改layout_gravitycenter_horizontal只爲LIN2LinearLayout

android:layout_gravity="center_horizontal" 

我不明白你爲什麼還要把center_vertical當父ScrollView高度設置爲wrap_content

+0

@Dorian你也可以在循環中使用「\ n」。 for(int i = 0; i jayellos

+0

謝謝! Luksprog。現在它可以工作。 – Dorian

+0

謝謝! marvz - 你有權利 – Dorian