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