如何使线性布局在动态添加其内容时自动滚动

发布于 2024-11-15 15:07:20 字数 1674 浏览 2 评论 0原文

我的应用程序包含不同的布局。其中一个是线性布局。它的内容是动态添加的。我想让这个布局在添加其内容时水平滚动。为此我编写了下面给出的代码..

 <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>

它正在工作..但是我想要在添加内容时自动滚动它...任何人都可以帮助我...

更多源代码:

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

        }

在这种方法中,它正在向我的布局添加更多内容...

My application containing different layouts.one of them is a linear layout.it's content is dynamically adding.i want to make this layout horizontally scrollable while adding its content.for that i wrote the code given below..

 <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>

it is working..but i want to scroll it automatically while adding contents...can anybody help me plz...

more source code:

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

        }

in this method it is adding more contents to my layout...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-11-22 15:07:20

作为父布局...

Put <ScrollView> as parent layout...

定格我的天空 2024-11-22 15:07:20

添加新元素时,您必须更新 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);

            }
        });

谢谢
迪帕克

You have to update your UI when new element is added

first initialize HorizonatlScrollView using the following code

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

when a new element is added use the following line there to scroll your HorizontalScrollView

runOnUiThread(new Runnable() {

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

            }
        });

Thanks
Deepak

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文