2011-10-09 105 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);