我想在我的应用程序中使用scrollview。我试图在scrollview中添加一个文本视图,但除了滚动视图的背景色之外,我看不到任何呈现的内容。无法以编程方式创建Android滚动视图。
这里是我是如何做的:
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);
}
}
我发现几乎所有的滚动型教程使用XML来定义视图。但我想以程序化的方式来完成。但无论如何,我也尝试过xml。
我复制罗曼盖伊的XML从这里来进行测试:HTTP://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
本身的XML滚动型是正确的,如果我创建这个滚动视图,并将其添加到活动中,使用
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的任何例子?
另外,那些java的java源代码也在kernel.org上?自从git服务关闭以后,我可以在哪里下载android源代码?