android:在java文件中使用weightviews为linearlayout的textviews

时间:2017-03-07 12:02:50

标签: java android android-layout android-layout-weight

有没有办法为java文件中的linearlayout的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
  

但是我在不同的移动版本中获得了未分配的textview。是的   有没有办法增加java文件的权重。自己做了字符串   concatination我无法使用权​​重

这是我得到的输出

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以从以下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);

答案 1 :(得分:0)

权重可以将文本视图调整为不同的高度。我严重怀疑这是你想要的。 如果我理解你的"与blankspace"对齐,那么你正在寻找如何应用等宽字体,以便小时数对齐。 祝你好运。