2017-03-07 48 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