我正在尝试制作一个“计划”应用程序,并且这样做的目的是使“计划”屏幕的TextView和Views表示活动可以对应的时隙。

但是,我很难使生成的TextView(将在Schedule上代表活动)正确地与与Activity的开始时间关联的View对齐。

例如,在这种情况下,我使用text = "CSI2120"创建一个TextView,并尝试将其与此处的“ 13:00” TextView上方的行(这是一个View)对齐。但是,我无法从这些链接中获取一些建议,因为它们无法正常工作。

Can I set “android:layout_below” at runtime, programmatically?

Android How to change layout_below to TextView programmatically(请参阅答案中的第二种方式)

TextView位于右上角的默认位置,而不是我想要的位置。应该做什么而不是链接建议什么?

java - 如何以编程方式编辑TextView参数?-LMLPHP

这是我的完整方法。 timeSlots是R.id.view [####] int的数组,而Schedule是该方法所在的Activity:

public void displayDailyActivities(int[] timeSlots){
    String[] todaysActivities = {"CSI2120", "01", "14", "2017", "0300", "0400"};

    // Make the associated TextView(s)
    for(int i=0; i < todaysActivities.length; i=i+6){

        int startTime = Integer.valueOf(todaysActivities[i+4]);
        int startTimeHour = startTime / 100;
        int startTimeMin = startTime % 100;

        // Make the TextView and add it to the Schedule

        // Code I got from links
        TextView newActivity = new TextView(Schedule.this);
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

        // Make the activity, grabbing the corresponding timeslot (View) from the timeSlots array
        newActivity.setText(todaysActivities[i]);

        // In this case ( timeSlots[startTimeHour + 1] ) returns ( R.id.view0300 )
        // which is the View (line) on the Schedule directly above to the "03:00" TextView
        relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
        newActivity.setLayoutParams(relativeParams);

        linearParams.setMargins(0, startTimeMin-3, 0, 0);
        newActivity.setLayoutParams(linearParams);

        // Add to the screen
        RelativeLayout schedule = (RelativeLayout) findViewById(R.id.scheduleView);
        schedule.addView(newActivity);

        // Make sure we're not going out of bounds
        if(i + 6 > todaysActivities.length){
            i = todaysActivities.length;
        }
        else{

        }
    }
}


编辑:我从其他类似问题中找到的代码,特别是对我不起作用的是以下几行:

            TextView newActivity = new TextView(Schedule.this);
            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);

             ...

            relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
            newActivity.setLayoutParams(relativeParams);

            linearParams.setMargins(0, startTimeMin-3, 0, 0);
            newActivity.setLayoutParams(linearParams);

最佳答案

您正在使用linearParams覆盖relativeParams。设置margin到relativeParms变量iteself,然后设置LayoutParams为newActivity,如下所示:

    relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
    relativeParams.setMargins(0, startTimeMin-3, 0, 0);
    newActivity.setLayoutParams(relativeParams);
    // Add to the screen
    RelativeLayout schedule = (RelativeLayout) findViewById(R.id.scheduleView);
    schedule.addView(newActivity);

07-24 22:27