如何在ScrollView中设置多个视图?

时间:2014-03-12 20:46:21

标签: android android-layout view scrollview

我正在使用ScrollView,我想在ScrollView中设置一些按钮或一些textview或edittext ..这是我的类MyScrollView谁是ScrollView类的子代:

public  class MyScrollView extends ScrollView {

private JSONParser  jParser;
private JSONObject  contenuScrollView;
private Context     context;

@SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
    super(context);

    this.context = context;
}

public void onCreate(Bundle savedInstanceState) {

}

/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, RelativeLayout rl) throws JSONException {
        if (listLabel.length() > 0)
            {
            DisplayMetrics metrics = new DisplayMetrics();
            for (int i = 0; i < listLabel.length(); i++) {
                LinearLayout ll = new LinearLayout(getContext());
                ll.setOrientation(LinearLayout.HORIZONTAL);

                TextView tv = new TextView(context);
                tv.setText(listLabel.getJSONObject(i).getString("text"));
                tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
                tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
                                                                                (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
                params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
                params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
                ll.addView(tv);
                //rl.addView(ll);
                this.addView(ll);
            }
        }
    }
}

我的问题是下一个:我可以用我的方法setClassicLabel设置一个Label,结果如下: enter image description here

当我试图设置第二个标签时,我有一个消息错误,他告诉我这个:

03-12 16:41:11.054: E/AndroidRuntime(5355): FATAL EXCEPTION: main
03-12 16:41:11.054: E/AndroidRuntime(5355): Process: com.fchps.bya, PID: 5355
03-12 16:41:11.054: E/AndroidRuntime(5355): java.lang.RuntimeException: Unable to start       activity ComponentInfo{com.fchps.bya/com.fchps.bya.classicview.ClassicView}:   java.lang.IllegalStateException: ScrollView can host only one direct child
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.os.Looper.loop(Looper.java:136)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at java.lang.reflect.Method.invoke(Method.java:515)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at dalvik.system.NativeStart.main(Native Method)
03-12 16:41:11.054: E/AndroidRuntime(5355): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.widget.ScrollView.addView(ScrollView.java:237)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at   com.fchps.bya.myScrollView.MyScrollView.setClassicLabel(MyScrollView.java:57)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.fchps.bya.classicview.ClassicView.setClassicScrollView(ClassicView.java:136)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.fchps.bya.classicview.ClassicView.onCreate(ClassicView.java:106)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.Activity.performCreate(Activity.java:5231)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-12 16:41:11.054: E/AndroidRuntime(5355):     ... 11 more

有人知道我做错了什么?感谢

2 个答案:

答案 0 :(得分:1)

你只能设置一个包含要在滚动视图中滚动的所有内容的视图你的for循环在循环的每次迭代中创建一个线性布局并添加它,你只能添加1,所以make 1线性布局然后在for循环将其他所有内容添加到linearlayout

类似这样的事情

public void setClassicLabel(JSONArray listLabel, RelativeLayout rl) throws JSONException {
        if (listLabel.length() > 0)
            {
            DisplayMetrics metrics = new DisplayMetrics();
            LinearLayout ll = new LinearLayout(getContext());
            ll.setOrientation(LinearLayout.HORIZONTAL);
            for (int i = 0; i < listLabel.length(); i++) {



                TextView tv = new TextView(context);
                tv.setText(listLabel.getJSONObject(i).getString("text"));
                tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
                tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
                                                                                (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
                params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
                params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
                ll.addView(tv);
                //rl.addView(ll);

            }
this.addView(ll);
        }
    }
}

因此您只需添加一次linearlayout,线性布局中包含所有文本视图

答案 1 :(得分:0)

通过调用this.addView(ll),您可以向ScrollView主机添加视图。但是ScrollView有一个限制 - 它只能有一个孩子(在LogCat中明确表示),因此setClassicLabel的第二次调用将失败(您试图再添加一个孩子)。解决方案是什么?在mContainer初始化期间向ScrollView添加容器mContainer.addView(ll)(类型为LinearLayout,RelativeLayout等),然后将视图添加到此容器{{1}}。