2011-12-23 23 views
2

我正在寫一個Android應用程序有它包含的LinearLayout滾動型:改變滾動視圖的位置在Android的

<ScrollView android:id="@+id/ScrollView01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity=top" 
     android:layout_marginTop="240dip" 
     android:layout_marginRight="0dip" 
     android:layout_marginBottom="60dip"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/centerlayout" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="top" 
       android:layout_marginRight="20dip" 
       android:layout_marginLeft="10dip" 
      android:layout_marginBottom="120dip"> 

正如你看到的,滾動型的保證金頂部是「240dip」。我希望能夠在我的Java代碼中進行更改。我試過這個:

ScrollView scrollView=(ScrollView)findViewById(R.id.ScrollView01); 
LayoutParams params=scrollView.getLayoutParams(); 

但我不能這樣做:params.top = 100;你能幫我怎麼做嗎?

回答

4

你是什麼意思,你「不能這樣做」。您需要執行params.top=100;然後scrollView.setLayoutParams(params);

但是你可以將其更改爲:

ScrollView scrollView=(ScrollView)findViewById(R.id.ScrollView01); 
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(scrollView.getLayoutParams()); 
params.topMargin = 100; 
scrollView.setLayoutParams(params); 
1

嘗試

params.gravity = Gravity.LEFT | Gravity.TOP; 

沒有你都會有問題就行了。

相關問題