android : 在 java 文件中对线性布局的文本视图使用权重

android : using weight to textviews for linearlayout in java file

在 java 文件中,是否有任何方法可以为线性布局的文本视图生成权重。

This is the code im using but its not alligned properly

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


    }

I have alligned the code using blankspace, this is not the proper way of doing since im getting different allignment in different mobile versions

需要输出:

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

But i am getting unalligned textview in different mobile versions.Is there any way of adding weight to java file.Since im doing string concatination i am unable to use weights

这是我得到的输出

enter image description here

您可以从 java 代码中设置 layoutweightweightSum,如下所示:

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

权重可以调整文本视图以具有不同的高度。我严重怀疑这就是你想要的。 如果我理解你的 "align with blankspace",你正在寻找如何应用等宽字体,以便时间对齐。 祝你好运。