滚动型的Andr​​oid不能以编程方式创建。 [英] Android scrollview can't be created programmatically.

查看:124
本文介绍了滚动型的Andr​​oid不能以编程方式创建。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序使用滚动视图。我尝试添加一个文本视图进入滚动视图,但我看不到任何东西呈现,除了滚动视图的背景颜色。

I want to use scrollview in my app. I tried to add a text view into the scrollview, but I can't see anything rendered, except the background color of the scroll view.

下面是我做的:

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。

I found almost all the scrollview tutorial are using xml to define the view. But I want to do it in a programmatic way. But anyway, I also tried xml.

我复制罗曼盖伊的XML从这里来进行测试:HTTP://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

I copied Romain Guy's xml from here for testing :http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

本身的XML滚动型是正确的,如果我创建这个滚动视图,并将其添加到活动中,使用

The xml scrollview by itself is correct, if I create this scrollview and add it to the activity, using

scrollview= (ScrollView)  getLayoutInflater().inflate(R.layout.scrollviewid,null);

的setContentView(滚动型);

setContentView(scrollview);

的setContentView(R.layout.scrollviewid);

它的工作。不过,如果我想使滚动视图其它视图的子视图中,再次我只能看到滚动视图的背景。里面什么也没有呈现:

it worked. However, if I want to make the scrollview a child view of some other view, again I could only see the background of the scrollview. nothing inside is rendered:

     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);
}
}

这有什么错我的code?是否与程序不XML创建滚动型的例子吗?

What's wrong with my code? Is there any example of creating scrollview with program not xml?

此外,为Android也是那些java源$ C ​​$ c。在kernel.org?因为git的服务下来,我在哪里可以下载Android源$ C ​​$ C?

Also, are those java source code of android also at kernel.org? since the git service is down, where can I download android source code?

推荐答案

当您以编程方式创建一个滚动型它里面你需要创建一个查看然后添加滚动型这里面查看

When you programmatically create a ScrollView inside of it you need to create a View then add the ScrollView inside this 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);

这篇关于滚动型的Andr​​oid不能以编程方式创建。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆