如何解决如何动态地添加线性内容,同时又动态地添加线性内容
| 我的应用程序包含不同的布局。其中一个是线性布局。它的内容是动态添加的。我想在添加其内容的同时使此布局水平滚动。为此,我编写了以下代码。 <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);
}
在这种方法中,它为我的布局添加了更多内容...
解决方法
添加新元素时,您必须更新UI
首先使用以下代码初始化HorizonatlScrollView
HorizontalScrollView s = (HorizontalScrollView) findViewById(R.id.HorizontalScrollView01);
当添加新元素时,使用以下行滚动HorizontalScrollView
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
谢谢
迪帕克
, 将ѭ4作为父级布局...
, 有一个scrollTo方法,请参见http://developer.android.com/reference/android/widget/HorizontalScrollView.html#scrollTo(int,int)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。