我想做一個調度應用程序,並在這樣做了一個與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的是在右上角的默認位置,不要在那裏我希望它是。應該做什麼而不是鏈接建議?
這裏是我的全部方法。 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);
哦,我的上帝,現在我覺得愚蠢。非常感謝你的回答,你不知道你剛剛救了我多少時間。 – gsamerica
我很高興幫助你:)快樂編碼:) – sravs