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

时间:2017-04-04 01:14:25

标签: java android android-layout textview

我正在尝试创建一个Scheduling app,并且这样做了一个Schedule屏幕,其中TextViews和Views代表了一个活动可以对应的时间段。

但是,我很难获得生成的TextView(它将代表计划中的活动),以便与与活动的开始时间相关联的视图正确排列。

例如,在这种情况下,我使用text = "CSI2120"创建一个TextView,并尝试使用" 13:00"上方的行(这是一个视图)对其进行排列。 TextView在这里。但是我错过了这些链接的建议,因为我无法让它们发挥作用。

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

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

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

enter image description here

这是我的完整方法。 timeSlots是一个R.id.view [####]整数数组,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);

2 个答案:

答案 0 :(得分:1)

你用linearParams覆盖了relativeParams。将margin设置为relativeParms变量iteself,然后将setLayoutParams设置为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);

答案 1 :(得分:0)

这可以解决您的问题

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setLayoutParams(params);
相关问题