2011-06-15 66 views
1

我的應用程序包含不同的佈局。其中一個是線性佈局。它的內容是動態添加的。我希望在添加內容時使此佈局水平滾動。我寫了下面給出的代碼..如何使線性佈局在動態添加內容時自動滾動

<LinearLayout android:id="@+id/scoreballparent_layout" 
    android:layout_height="wrap_content" 
android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_above="@+id/score_layout"> 
    <HorizontalScrollView android:layout_height="wrap_content" 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    > 
     <LinearLayout android:layout_width="fill_parent" 
     android:id="@+id/scoreball_layout" 
     android:layout_height="wrap_content" 
     > 

     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 

是working..but我想自動滾動它同時增加了內容......任何人可以幫助我PLZ ...

多個源代碼:

private void scoreball_display(String score) 
    { 
     addscoreball = new Button(getApplicationContext()); 
     addscoreball.setId(134); 
     if(score=="WD" || score=="NB") 
     { 
      addscoreball.setTextAppearance(this,R.style.plainText); 
     } 
     else{ 
      addscoreball.setTextAppearance(this,R.style.BoldText); 
     } 

     addscoreball.setText(score); 
     addscoreball.setSingleLine(true); 
     addscoreball.setBackgroundDrawable(getResources().getDrawable  (R.drawable.white_ball)); 
     addscoreball.setGravity(Gravity.CENTER_HORIZONTAL); 
     addscoreball.setGravity(Gravity.CENTER_VERTICAL); 
     LinearLayout.LayoutParams addscoreball_Params = 
      new LinearLayout.LayoutParams(35,35); 
     scoreballlayout.addView(addscoreball,addscoreball_Params); 

     } 
在這種方法是增加更多的內容,我的佈局

...

回答

1

添加新元素時必須更新UI

首先使用以下代碼初始化Horizo​​natlScrollView

HorizontalScrollView s = (HorizontalScrollView) findViewById(R.id.HorizontalScrollView01); 

當一個新的元素添加使用下面的行滾動您Horizo​​ntalScrollView

runOnUiThread(new Runnable() { 

      public void run() { 
       // TODO Auto-generated method stub 
       s.fullScroll(HorizontalScrollView.FOCUS_RIGHT); 

      } 
     }); 

感謝 迪帕克

+0

我試過但不工作 – 2011-06-15 08:26:40

+0

你能發表更多源代碼嗎? – 2011-06-15 09:08:02

+0

我貼過多個源代碼 – 2011-06-15 13:09:01

3

<ScrollView>父佈局...

+0

ü意味着水平滾動視圖的父佈局? – 2011-06-15 08:02:50

相關問題