2017-04-04 15 views
1

私はスケジューリングアプリケーションを作成しようとしています。そうすることで、アクティビティが対応できるタイムスロットを表すTextViewsとViewsを持つスケジュール画面が作成されました。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 [####] intの配列であり、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

を解決することができます。その後、セットrelativeParms変数iteselfに余白と以下のようにnewActivityにsetLayoutParams:

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

これは、あなたがlinearParamsとrelativeParamsをオーバーライドしているあなたの問題

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

は、残念ながら、私はこの1つに似ているように、ここで他の質問にこの答えを見てきましたが、それは私のために動作しません。また、代わりに 'ViewGroup.LayoutParams'ですか? 'LayoutParams'の代わりに? そして、何 について 'relativeParams.addRule(RelativeLayout.BELOW、タイムスロット[startTimeHour + 1]);' 'newActivity.setLayoutParams(relativeParams);' 私のコードから?このコードがコンパイルされている間は、TextViewを必要な場所に設定しません。代わりに私は何をしなければなりませんか? – gsamerica

関連する問題