【问题标题】:to add rows and textviews to each day of the table dynamically动态地将行和文本视图添加到表格的每一天
【发布时间】:2016-09-18 11:39:41
【问题描述】:
public class TimeTableActivity extends AppCompatActivity {

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


        TableLayout activity_time_table = new TableLayout(this);

        activity_time_table.setStretchAllColumns(true);
        activity_time_table.setShrinkAllColumns(true);

        TableRow rowTitle = new TableRow(this);
        rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);

        TableRow rowDayLabels = new TableRow(this);
        TableRow monday = new TableRow(this);
        TableRow tuesday = new TableRow(this);
        TableRow wednesday=new TableRow(this);
        TableRow thursday=new TableRow(this);
        TableRow friday=new TableRow(this);
        TableRow saturday=new TableRow(this);

        TextView empty = new TextView(this);

        // title column/row
        TextView title = new TextView(this);
        title.setText("Time Table");

        title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        title.setGravity(Gravity.CENTER);
        title.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TableRow.LayoutParams params = new TableRow.LayoutParams();
        params.span = 6;

        rowTitle.addView(title, params);

        // labels column
        TextView monLabel = new TextView(this);
        monLabel.setText("Monday");
        monLabel.setTypeface(Typeface.DEFAULT_BOLD);

        TextView tueLabel = new TextView(this);
        tueLabel.setText("Tuesday");
        tueLabel.setTypeface(Typeface.DEFAULT_BOLD);

        TextView wedLabel=new TextView(this);
        wedLabel.setText("Wednesday");
        wedLabel.setTypeface(Typeface.DEFAULT_BOLD);

        TextView thurLabel=new TextView(this);
        thurLabel.setText("Thursday");
        thurLabel.setTypeface(Typeface.DEFAULT_BOLD);

        TextView friLabel=new TextView(this);
        friLabel.setText("Friday");
        friLabel.setTypeface(Typeface.DEFAULT_BOLD);

        TextView satLabel=new TextView(this);
        satLabel.setText("Saturday");
        satLabel.setTypeface(Typeface.DEFAULT_BOLD);

        rowDayLabels.addView(empty);
        monday.addView(monLabel);
        tuesday.addView(tueLabel);
        wednesday.addView(wedLabel);
        thursday.addView(thurLabel);
        friday.addView(friLabel);
        saturday.addView(satLabel);

        // day 1 column
        //add days for thursday friday satruday rahul midnyt
        TextView day1Label = new TextView(this);
        day1Label.setText("09:00-09:55");
        day1Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon1 = new TextView(this);
        mon1.setText("6");
        mon1.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue1 = new TextView(this);
        tue1.setText("7/8");
        tue1.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed1=new TextView(this);
        wed1.setText("4");
        wed1.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur1=new TextView(this);
        thur1.setText("3");
        thur1.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri1=new TextView(this);
        fri1.setText("1");
        fri1.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView sat1=new TextView(this);
        sat1.setText("3");
        sat1.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day1Label);
        monday.addView(mon1);
        tuesday.addView(tue1);
        wednesday.addView(wed1);
        thursday.addView(thur1);
        friday.addView(fri1);
        saturday.addView(sat1);

        // day2 column
        TextView day2Label = new TextView(this);
        day2Label.setText("09:55-10:50");
        day2Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon2 = new TextView(this);
        mon2.setText("4");
        mon2.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue2 = new TextView(this);
        tue2.setText("7/8");
        tue2.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed2=new TextView(this);
        wed2.setText("2");
        wed2.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur2=new TextView(this);
        thur2.setText("5");
        thur2.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri2=new TextView(this);
        fri2.setText("3");
        fri2.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView sat2=new TextView(this);
        sat2.setText("1");
        sat2.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day2Label);
        monday.addView(mon2);
        tuesday.addView(tue2);
        wednesday.addView(wed2);
        thursday.addView(thur2);
        friday.addView(fri2);
        saturday.addView(sat2);

        // day3 column
        TextView day3Label = new TextView(this);
        day3Label.setText("11:10-12:05");
        day3Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon3 = new TextView(this);
        mon3.setText("1");
        mon3.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue3 = new TextView(this);
        tue3.setText("7/8");
        tue3.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed3=new TextView(this);
        wed3.setText("5");
        wed3.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur3=new TextView(this);
        thur3.setText("6");
        thur3.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri3=new TextView(this);
        fri3.setText("2");
        fri3.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView sat3=new TextView(this);
        sat3.setText("-");
        sat3.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day3Label);
        monday.addView(mon3);
        tuesday.addView(tue3);
        wednesday.addView(wed3);
        thursday.addView(thur3);
        friday.addView(fri3);
        saturday.addView(sat3);

        // day4 column
        TextView day4Label = new TextView(this);
        day4Label.setText("12:05-01:00");
        day4Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon4 = new TextView(this);
        mon4.setText("3");
        mon4.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue4 = new TextView(this);
        tue4.setText("6");
        tue4.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed4=new TextView(this);
        wed4.setText("3");
        wed4.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur4=new TextView(this);
        thur4.setText("4");
        thur4.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri4=new TextView(this);
        fri4.setText("5");
        fri4.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView sat4=new TextView(this);
        sat4.setText("-");
        sat4.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day4Label);
        monday.addView(mon4);
        tuesday.addView(tue4);
        wednesday.addView(wed4);
        thursday.addView(thur4);
        friday.addView(fri4);
        saturday.addView(sat4);

        // day5 column
        TextView day5Label = new TextView(this);
        day5Label.setText("01:55-02:50");
        day5Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon5 = new TextView(this);
        mon5.setText("2");
        mon5.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue5 = new TextView(this);
        tue5.setText("4");
        tue5.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed5=new TextView(this);
        wed5.setText("6");
        wed5.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur5=new TextView(this);
        thur5.setText("1");
        thur5.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri5=new TextView(this);
        fri5.setText("7/8");
        fri5.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day5Label);
        monday.addView(mon5);
        tuesday.addView(tue5);
        wednesday.addView(wed5);
        thursday.addView(thur5);
        friday.addView(fri5);

        // day6 column
        TextView day6Label = new TextView(this);
        day6Label.setText("02:50-03:45");
        day6Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon6 = new TextView(this);
        mon6.setText("R");
        mon6.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue6 = new TextView(this);
        tue6.setText("2");
        tue6.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed6=new TextView(this);
        wed6.setText("T");
        wed6.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur6=new TextView(this);
        thur6.setText("R");
        thur6.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri6=new TextView(this);
        fri6.setText("7/8");
        fri6.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day6Label);
        monday.addView(mon6);
        tuesday.addView(tue6);
        wednesday.addView(wed6);
        thursday.addView(thur6);
        friday.addView(fri6);

        // day7 column
        TextView day7Label = new TextView(this);
        day7Label.setText("03:45-04:40");
        day7Label.setTypeface(Typeface.SERIF, Typeface.BOLD);

        TextView mon7 = new TextView(this);
        mon7.setText("-");
        mon7.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView tue7 = new TextView(this);
        tue7.setText("-");
        tue7.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView wed7=new TextView(this);
        wed7.setText("-");
        wed7.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView thur7=new TextView(this);
        thur7.setText("-");
        thur7.setGravity(Gravity.CENTER_HORIZONTAL);

        TextView fri7=new TextView(this);
        fri7.setText("7/8");
        fri7.setGravity(Gravity.CENTER_HORIZONTAL);

        rowDayLabels.addView(day7Label);
        monday.addView(mon7);
        tuesday.addView(tue7);
        wednesday.addView(wed7);
        thursday.addView(thur7);
        friday.addView(fri7);

        activity_time_table.addView(rowTitle);
        activity_time_table.addView(rowDayLabels);
        activity_time_table.addView(monday);
        activity_time_table.addView(tuesday);
        activity_time_table.addView(wednesday);
        activity_time_table.addView(thursday);
        activity_time_table.addView(friday);
        activity_time_table.addView(saturday);

        setContentView(activity_time_table);

    }
}

如何动态地向该表中添加行并为每一行插入文本视图,而不是每小时编写代码..如何使用 for 循环来完成.. 具有 7/8 的列应该占用 3 的跨度。 提前考虑

【问题讨论】:

    标签: android android-layout tablelayout android-tablelayout


    【解决方案1】:

    不要使用表格布局,而是使用列表视图和自定义适配器

    【讨论】:

    • 如果我使用列表视图,那么我不能在 7/8(文本视图)上使用跨度 3,其中每 7/8 应该占用 3 个列..
    • 我想在表格上显示的数字应该可以放在任何行和任何列上
    • @gabriel14311 - 你的结果是什么,我可以看看任何线框或设计
    • 它将在表格布局中显示 textviews ...它将使用 tablerow 插入 textview ..这里我已经设置了每个 textview 的文本每次它是静态的.intead 我想初始化一次并使用 count = 4 我想将文本视图一一插入到表中,并且没有相同数字的文本视图 shld 不是 cme 在同一行中,并且每天 shld 总共仅包含 6 个数字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多