繁体   English   中英

android:在Java文件中使用权重对textviews进行linearlayout

[英]android : using weight to textviews for linearlayout in java file

有什么方法可以为Java文件中的linearlayout生成文本视图的权重。

这是我正在使用的代码,但未正确指定

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


    }

我已经使用空格指定了代码,这不是正确的做法,因为即时通讯在不同的移动版本中获得了不同的指定

需要的输出:

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

但是我在不同的移动版本中得到了未经许可的文本视图。是否有任何方法可以增加Java文件的权重。由于我正在做字符串隐喻,所以我无法使用权​​重

这是我得到的输出

在此处输入图片说明

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

权重可以将textview调整为不同的高度。 我严重怀疑这就是您想要的。 如果我了解您的“与空格对齐”,那么您正在寻找如何应用等宽字体,以便使时间对齐。 祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM