【问题标题】:Inflating 5 linear layouts in scrollview overwriting each other在滚动视图中膨胀 5 个线性布局相互覆盖
【发布时间】:2015-08-28 03:26:13
【问题描述】:

我目前正在开发一个 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 个漂亮的小线性布局在哪里。

【问题讨论】:

  • 尝试用 LinearLayout 替换您的 RelativeLayout 并将其方向设置为垂直。
  • @HusseinElFeky 成功了!非常感谢

标签: android android-layout android-linearlayout android-scrollview


【解决方案1】:

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

LinearLayout 会自动排列你的视图,但是如果你想使用一个RelativeLayout,那么你必须为你在RelativeLayout 中的每个视图添加android:layout_below 属性。所以 LinearLayout 显然更简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多