在滚动视图中膨胀 5 个线性布局相互覆盖

Inflating 5 linear layouts in scrollview overwriting each other

我目前正在开发一个时间 table 应用程序,时间为 5 天。我的想法是在相对布局中放置 5 个线性布局,在滚动视图中放置相对布局,因为它不适合屏幕。

我做了我的 xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="schouten.jamie.maantje.com.roosternotifierfriesepoort.Activities.RoosterActivity">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/maandag"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dinsdag"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/woensdag"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/donderdag"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/vrijdag"
        android:orientation="vertical">
    </LinearLayout>
</RelativeLayout>

</ScrollView>

之后我开始写代码:

for (int i = 0; i < maandag.length(); i++) {
            JSONObject maandagJSONObject = maandag.getJSONObject(i);

            LayoutInflater inflater = (LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout main =(LinearLayout)findViewById(R.id.maandag);

            View view = inflater.inflate(R.layout.roosteruur, main, false);

            TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
            TextView les = (TextView)view.findViewById(R.id.vak);
            TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
            TextView uur = (TextView)view.findViewById(R.id.uur);

            uur.setText(String.valueOf(i + 1)+")");
            lokaal.setText(maandagJSONObject.getString("lokaal"));
            les.setText(maandagJSONObject.getString("les"));
            leerkracht.setText(maandagJSONObject.getString("leraar"));

            Log.v("roosterp", maandagJSONObject.getString("les"));

            main.addView(view);

}

运行 它而且效果很好所以我想再复制并粘贴 4 次并更改日期并完成!

所以我就是这样做的:

public void parseJSON(String json){
    Log.v("JPARSER","parseJSON start");
    try {
        JSONObject objects = new JSONObject(json);

        JSONArray maandag = objects.getJSONArray("maandag");
        JSONArray dinsdag = objects.getJSONArray("dinsdag");
        JSONArray woensdag = objects.getJSONArray("woensdag");
        JSONArray donderdag = objects.getJSONArray("donderdag");
        JSONArray vrijdag = objects.getJSONArray("vrijdag");



        for (int i = 0; i < maandag.length(); i++) {
            JSONObject maandagJSONObject = maandag.getJSONObject(i);

            LayoutInflater inflater = (LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout main =(LinearLayout)findViewById(R.id.maandag);

            View view = inflater.inflate(R.layout.roosteruur, main, false);

            TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
            TextView les = (TextView)view.findViewById(R.id.vak);
            TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
            TextView uur = (TextView)view.findViewById(R.id.uur);

            uur.setText(String.valueOf(i + 1)+")");
            lokaal.setText(maandagJSONObject.getString("lokaal"));
            les.setText(maandagJSONObject.getString("les"));
            leerkracht.setText(maandagJSONObject.getString("leraar"));

            Log.v("roosterp", maandagJSONObject.getString("les"));

            main.addView(view);

        }

        for (int i = 0; i < dinsdag.length(); i++) {
            JSONObject dinsdagJSONObject = dinsdag.getJSONObject(i);

            LayoutInflater inflater = (LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout main =(LinearLayout)findViewById(R.id.maandag);

            View view = inflater.inflate(R.layout.roosteruur, main, false);

            TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
            TextView les = (TextView)view.findViewById(R.id.vak);
            TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
            TextView uur = (TextView)view.findViewById(R.id.uur);

            uur.setText(String.valueOf(i + 1)+")");
            lokaal.setText(dinsdagJSONObject.getString("lokaal"));
            les.setText(dinsdagJSONObject.getString("les"));
            leerkracht.setText(dinsdagJSONObject.getString("leraar"));
            Log.v("roosterp", dinsdagJSONObject.getString("les"));

            main.addView(view);

        }

        for (int i = 0; i < woensdag.length(); i++) {
            JSONObject woensdagJSONObject = woensdag.getJSONObject(i);

            LayoutInflater inflater = (LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout main =(LinearLayout)findViewById(R.id.maandag);

            View view = inflater.inflate(R.layout.roosteruur, main, false);

            TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
            TextView les = (TextView)view.findViewById(R.id.vak);
            TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
            TextView uur = (TextView)view.findViewById(R.id.uur);

            uur.setText(String.valueOf(i + 1)+")");
            lokaal.setText(woensdagJSONObject.getString("lokaal"));
            les.setText(woensdagJSONObject.getString("les"));
            leerkracht.setText(woensdagJSONObject.getString("leraar"));
            Log.v("roosterp", woensdagJSONObject.getString("les"));

            main.addView(view);

        }

        for (int i = 0; i < donderdag.length(); i++) {
            JSONObject donderdagJSONObject = donderdag.getJSONObject(i);

            LayoutInflater inflater = (LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout main =(LinearLayout)findViewById(R.id.maandag);

            View view = inflater.inflate(R.layout.roosteruur, main, false);

            TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
            TextView les = (TextView)view.findViewById(R.id.vak);
            TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
            TextView uur = (TextView)view.findViewById(R.id.uur);

            uur.setText(String.valueOf(i + 1)+")");
            lokaal.setText(donderdagJSONObject.getString("lokaal"));
            les.setText(donderdagJSONObject.getString("les"));
            leerkracht.setText(donderdagJSONObject.getString("leraar"));
            Log.v("roosterp", donderdagJSONObject.getString("les"));

            main.addView(view);

        }

        for (int i = 0; i < vrijdag.length(); i++) {
            JSONObject vrijdagJSONObject = vrijdag.getJSONObject(i);

            LayoutInflater inflater = (LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout main =(LinearLayout)findViewById(R.id.maandag);

            View view = inflater.inflate(R.layout.roosteruur, main, false);

            TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
            TextView les = (TextView)view.findViewById(R.id.vak);
            TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
            TextView uur = (TextView)view.findViewById(R.id.uur);

            uur.setText(String.valueOf(i + 1)+")");
            lokaal.setText(vrijdagJSONObject.getString("lokaal"));
            les.setText(vrijdagJSONObject.getString("les"));
            leerkracht.setText(vrijdagJSONObject.getString("leraar"));
            Log.v("roosterp", vrijdagJSONObject.getString("les"));

            main.addView(view);

        }


    }
    catch (JSONException e){
        Log.v("JPARSER","WUT? " + e);
    }


 }

但结果不是我所期待的,而不是 5 个漂亮的小线性布局,我得到的 wat 看起来像 1 只是与几天的数据混合,所以我猜有些东西在某处被覆盖但不能'找不到地方。

尝试将 RelativeLayout 替换为 LinearLayout 并将其方向设置为垂直。

LinearLayout 会自动排列您的视图,但如果您想使用 RelativeLayout,则必须向 RelativeLayout 中的每个视图添加 android:layout_below 属性。所以LinearLayout显然更简单