2017-04-04 82 views
1

我想做一個調度應用程序,並在這樣做了一個與TextViews和視圖,代表一個活動可以對應的時間表的時間表屏幕。如何以編程方式編輯TextView參數?

但是,我很難得到一個生成的TextView(它將表示活動的時間表)與活動的開始時間關聯的視圖正確排列。

例如,在這種情況下,我製作一個帶有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的是在右上角的默認位置,不要在那裏我希望它是。應該做什麼而不是鏈接建議?

enter image description here

這裏是我的全部方法。 timeSlots是R.id.view [####]整數數組和Schedule是活性的方法是在:

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); 

回答

1

要覆蓋與linearParams的relativeParams。設置頁邊距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); 
+0

哦,我的上帝,現在我覺得愚蠢。非常感謝你的回答,你不知道你剛剛救了我多少時間。 – gsamerica

+0

我很高興幫助你:)快樂編碼:) – sravs

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); 
+0

不幸的是我已經看到了這裏的SO其他問題類似於這一個這個答案,但它並沒有爲我工作。另外,它應該是'ViewGroup.LayoutParams'嗎?而不只是'LayoutParams'? 又是怎麼回事 'relativeParams.addRule(RelativeLayout.BELOW,時隙[startTimeHour + 1]);' 'newActivity.setLayoutParams(relativeParams);' 從我的代碼?雖然此代碼編譯時,它不會將TextView設置爲我想要的位置。我應該做些什麼呢? – gsamerica