2011-10-09 158 views
3

我想在我的应用程序中使用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源代码?

回答

1

我不清楚你的ViewGroup有什么问题,但这似乎是问题所在。如果我把你的代码,调试它(上面发布的代码有几个错误),并将其放入一个简单的活动的开始代码,它会按预期工作。它会创建一个带有测试文本的滚动文本区域。

这是代码。需要注意的是,预计的布局文件包含一个简单的线性布局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); 
}} 
3

当以编程方式创建ScrollView里面的它,你需要创建一个View然后添加ScrollView这里面View

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); 
相关问题