android - ScrollView如何设置多个 View ?

标签 android android-layout view scrollview

我正在使用 ScrollView,我想在我的 ScrollView 中设置一些按钮或一些 TextView 或编辑文本。这是我的类 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 设置一个标签,结果是这样的: 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

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

最佳答案

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

像这样

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

所以你只添加了一次线性布局,线性布局中包含了所有的 TextView

关于android - ScrollView如何设置多个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363433/

相关文章:

android - 如何配置警告对话的标题

mysql - 尝试加入多个表

asp.net-mvc - MVC查看可为空的日期字段格式

java - Dagger 2 - 两个提供相同接口(interface)的方法

android - Android FEST无法针对IDE中的单元测试正确编译

android - 最大化 ListView 中的可见行

Android:如何将图像稍微定位在 View 之外

Android toggleButton.setOnText 和 .invalidate 不刷新文本

用于对特定对象数组进行排序的 Java Comparator 类

java - 如何在 GridView 中更改图像?