Android scrollview可以';不能以编程方式创建。
我想在我的应用程序中使用scrollview。我试图将文本视图添加到scrollview中,但除了滚动视图的背景色之外,我看不到任何渲染的内容 我是这样做的:Android scrollview可以';不能以编程方式创建。,android,scrollview,Android,Scrollview,我想在我的应用程序中使用scrollview。我试图将文本视图添加到scrollview中,但除了滚动视图的背景色之外,我看不到任何渲染的内容 我是这样做的: public class MyView extends ViewGroup { ScrollView myScrollview; Textview tv; public MyView(Context context) { myScrollView = new ScrollView
public class MyView extends ViewGroup
{
ScrollView myScrollview;
Textview tv;
public MyView(Context context) {
myScrollView = new ScrollView(context);
myScrollView.setBackgroundColor(0xfff00fff);
textview=new TextView(context);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
textview.setLayoutParams(params)
textview.setText("sadfasdfasdfasdfasdfasdfasdfsadfsadf");
textview.layout(0, 0, 1000, 2000);
textview.setHeight(5000);
textview.setWidth(3200);
myScrollView .addView(tv);
addView(myScrollview);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int width = r-l;
int height =b-t;
myScrollView .layout(0, 0, width, height-100);
}
}
我发现几乎所有的scrollview教程都使用xml来定义视图。但我想以一种程序化的方式来做。但无论如何,我也尝试了xml
我从这里复制了Romain Guy的xml进行测试:http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
xml scrollview本身是正确的,如果我创建这个scrollview并使用
scrollview= (ScrollView) getLayoutInflater().inflate(R.layout.scrollviewid,null);
setContentView(滚动视图)
或setContentView(R.layout.scrollviewid)代码>
成功了。但是,如果我想使scrollview成为其他视图的子视图,我只能再次看到scrollview的背景。内部未呈现任何内容:
public class MyView extends ViewGroup
{
ScrollView myScrollview;
public MyView(Activity activity,Context context)
{
super(context);
myScrollview= (ScrollView) activity.getLayoutInflater().inflate(R.layout.restaurantcategoryselectorviewlayout,null);
myScrollview.setBackgroundColor(0xfff00fff);
addView(myScrollview);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int width = r-l;
int height =b-t;
myScrollview.layout(0, 0, width, height-100);
}
}
我的代码怎么了?有没有用程序而不是xml创建scrollview的例子
还有,android的java源代码也在kernel.org上吗?由于git服务已关闭,我在哪里可以下载android源代码?我不清楚您的视图组出了什么问题,但这似乎就是问题所在。如果我接受您的代码,调试它(上面发布的代码有几个错误),并将其放入一个简单活动的开始代码中,那么它将按预期工作。它使用测试文本创建一个滚动文本区域
这是密码。请注意,它希望布局文件包含一个简单的线性布局,id为linearLayout1
:
public class ListTestActivity extends Activity {
LinearLayout layout;
ScrollView myScrollView;
TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) this.findViewById(R.id.linearLayout1);
myScrollView = new ScrollView(this);
myScrollView.setBackgroundColor(0xfff00fff);
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
textview = new TextView(this);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
textview.setText("sadfasdfasdfasdfasdfasd fasdfsadfsadf");
myScrollView.addView(textview);
layout.addView(myScrollView);
}}
当以编程方式在其内部创建滚动视图时,需要创建视图
,然后在此视图
中添加滚动视图
LinearLayout maincontainer = (LinearLayout) findViewById(R.id.weatherInfo);
maincontainer.setOrientation(LinearLayout.HORIZONTAL);
final HorizontalScrollView scrollView = new HorizontalScrollView(getApplicationContext());
maincontainer.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(getApplicationContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
scrollView.addView(linearLayout);