# TableView Spaltenbreite Beibehalten



## Lucaaa (28. Nov 2018)

Hallo!
ich habe eine TableView mit TextViews.
Ich möchte, dass alle meine Spalten gleichbreit sind, jedoch wenn ich text hinzufüge, wird die Breite verändert. Dies darf NICHT passieren!


```
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".ScheduleFragment">

    <TableLayout
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:collapseColumns="*"
        android:shrinkColumns="*"></TableLayout>

</FrameLayout>
```


```
package com.ludevstudio.schoolmanager;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.ludevstudio.schoolmanager.Elements.Schedule;
import com.ludevstudio.schoolmanager.Elements.Subject;
import com.ludevstudio.schoolmanager.data.SchedulesSrc;
import com.ludevstudio.schoolmanager.data.SubjectsSrc;

import java.io.Serializable;


public class ScheduleFragment extends Fragment implements Serializable {
    Schedule schedule;
    SchedulesSrc schedulesSrc;
    SubjectsSrc subjectsSrc;
    int week;

    String[] weekDays;

    public ScheduleFragment() {
    }




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_schedule, container, false);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle variables = getArguments();
        schedule = (Schedule) variables.get("schedule");
        schedulesSrc = (SchedulesSrc) variables.get("schedulesrc");
        weekDays = getResources().getStringArray(R.array.weekdays);
        initUI();
    }

    public void initUI() {
        TableLayout grid = (TableLayout) getView().findViewById(R.id.grid);

        for(int h=0; h<schedule.getLessons(); h++) {
            TableRow row =new TableRow(getActivity());
           for(int i=0; i<6; i++) {
                if(h==0) {  // add day titles in first line
                    if(i!=0) {
                        ScheduleDayCell cell =new ScheduleDayCell(getContext(), weekDays[i-1]);
                        row.addView(cell);
                    } else {
                        ScheduleDayCell cell =new ScheduleDayCell(getContext(), "");
                       row.addView(cell);
                    }

                } else {
                   ScheduleCell item =new ScheduleCell(getContext(), new Subject(1, "test", "test", "test", Color.GRAY));
                    row.addView(item);
                }
            }
            grid.addView(row);

        }

    }



    public class ScheduleDayCell extends TextView {
        String day;

        public ScheduleDayCell (Context context, String day) {
            super(context);
            this.day = day;

         //   this.setText(day);

            // Settings
            this.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            this.setPadding(10,10,10,10);
            this.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTextWhite));
            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }


    }


    public class ScheduleCell extends LinearLayout {
        Subject subject;

        public ScheduleCell(Context context, Subject subject) {
            super(context);
        this.subject = subject;
        setBackgroundResource(R.drawable.schedule_item_border);


        TextView tvName =new TextView(context);
        tvName.setText(subject.getName());
        this.addView(tvName);

        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec); // force square shape
        }
    }


}
```



Hat wer eine Idee, wie ich das anstelle?


----------



## Robat (28. Nov 2018)

Hast du mal versucht die Breite der Elemente in den einzelnen Zeilen `0dip` zu setzen und `layout_weight="1"`?
Siehe
http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html
https://www.askingbox.com/tutorial/android-table-layout-with-columns-of-equal-width


----------



## Lucaaa (28. Nov 2018)

Robat hat gesagt.:


> Hast du mal versucht


Wie? Ich habe kein XML layout mit Unterelementen.


----------



## Robat (28. Nov 2018)

Kannst du über die LayoutParams einstellen.
TableLayout.LayoutParams


----------

