2017-03-07 45 views
0

有沒有什麼方法可以爲java文件中的linearlayout的textviews生成權重?android:在java文件中對線性佈局的textviews使用權重

這是使用代碼即時通訊,但它無法正常神韻:

的java文件:

LinearLayout afternoonLayout = (LinearLayout) findViewById(R.id.afternoonLayoutId); 

    if(afternoonSession !=null && afternoonSession.size() >= 0){ 

     TextView txtAfternoon = new TextView(this); 
     txtAfternoon.setText("Afternoon"); 
     txtAfternoon.setTextSize(22); 
     txtAfternoon.setGravity(Gravity.LEFT); 
     txtAfternoon.setTypeface(null, Typeface.BOLD); 
     txtAfternoon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     if(afternoonSession.has("Vitals")){ 

      txtAfternoonVitals = new TextView(this); 
      txtAfternoonVitals.setText("Vitals " + "- " + "      " + afternoonSession.get("Vitals").toString().replace("\"", "")); 
      txtAfternoonVitals.setTextSize(16); 
      txtAfternoonVitals.setGravity(Gravity.START); 
      txtAfternoonVitals.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     }else{ 
      txtAfternoonVitals = new TextView(this); 
      txtAfternoonVitals.setText("Vitals " + "- " + "      " + "Not Done"); 
      txtAfternoonVitals.setTextColor(Color.RED); 
      txtAfternoonVitals.setTextSize(16); 
      txtAfternoonVitals.setGravity(Gravity.START); 
      txtAfternoonVitals.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     } 

     if(afternoonSession.has("Lunch")){ 

      txtAfternoonLunch = new TextView(this); 
      txtAfternoonLunch.setText("Lunch " + "- " + "      " + afternoonSession.get("Lunch").toString().replace("\"", "")); 
      txtAfternoonLunch.setTextSize(16); 
      txtAfternoonLunch.setGravity(Gravity.START); 
      txtAfternoonLunch.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     }else{ 
      txtAfternoonLunch = new TextView(this); 
      txtAfternoonLunch.setText("Lunch " + "- " + "      " + "Not Done"); 
      txtAfternoonLunch.setTextColor(Color.RED); 
      txtAfternoonLunch.setTextSize(16); 
      txtAfternoonLunch.setGravity(Gravity.START); 
      txtAfternoonLunch.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     } 

     if(afternoonSession.has("Oral Medication")){ 

      txtAfternoonOral = new TextView(this); 
      txtAfternoonOral.setText("Oral Medication " + "- " + " " + afternoonSession.get("Oral Medication").toString().replace("\"", "")); 
      txtAfternoonOral.setTextSize(16); 
      txtAfternoonOral.setGravity(Gravity.START); 
      txtAfternoonOral.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     }else{ 
      txtAfternoonOral = new TextView(this); 
      txtAfternoonOral.setText("Oral Medication " + "- " + " " + "Not Done"); 
      txtAfternoonOral.setTextColor(Color.RED); 
      txtAfternoonOral.setTextSize(16); 
      txtAfternoonOral.setGravity(Gravity.START); 
      txtAfternoonOral.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     } 

     afternoonLayout.setPadding(10,10,10,10); 
     afternoonLayout.addView(txtAfternoon); 
     afternoonLayout.addView(txtAfternoonVitals); 
     afternoonLayout.addView(txtAfternoonLunch); 
     afternoonLayout.addView(txtAfternoonOral); 


    } 

我已經使用blankspace神韻:代碼,這是不正確的 由於即時通訊在不同移動版本中獲得不同的對齊方式 版本

輸出需要:

Afternoon 
    vitals   02:00pm 
    Lunch    03:00pm 
    Oral Medication 04:00pm 

但我在不同的移動versions.Is 有增加重量到Java file.Since即時通訊做串 concatination的任何方式獲得unalligned TextView的我我無法使用權​​

這是我收到

輸出

enter image description here

+1

爲什麼不使用recyclerview來達到這個目的,並創建一個適當的行,並且權重很容易滿足這個要求? –

+0

您也可以爲每個textView分配權重,並確保寬度和高度均設置爲match_parent – Janwilx72

+0

將第三個參數添加到setLayoutParams中,像這樣 - txtAfternoon.setLayoutParams(LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams。 WRAP_CONTENT,1F)); – Ragini

回答

0

權重可以調整的TextView要與不同的高度。我嚴重懷疑這是你想要的。 如果我理解你的「與空白對齊」,你正在尋找如何應用等寬字體,以便對齊時間。 祝你好運。

0

您可以設置layoutweightweightSum從Java代碼象下面這樣:

LinearLayout layout = (LinearLayout)findViewById(R.id.afternoonLayoutId); 
layout.setWeightSum(10F); 
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); 
//you can create new LayoutParams too ... 
layoutParams.weight = 0.5f; 
txtAfternoonVitals = new TextView(layout.getContext()); 
txtAfternoon .setLayoutParams(layoutParams); 
+0

Still它沒有被正確地分配。我嘗試設置佈局重量和權重。 – Abhinay

+0

因爲我在做字符串連接,所以我無法正確對齊 – Abhinay