如何将文本视图与tablerow对齐

时间:2016-10-23 05:07:24

标签: android textview android-tablelayout

我错了,无法自行解决的问题。

这里,我创建了4个文本视图,并在运行时将其添加到表行。 文本视图的宽度与标题列wdith相等,文本视图的高度固定为40dip。当数据在textview的宽度范围内时,一切看起来都很好。但是当数据超过textview的宽度时会截断。

Textview未与屏幕截图中的tablerow(父级)对齐。

https://i.stack.imgur.com/USFlb.jpg

任何建议都会非常感激。

我的代码:

 public void initi() {

    Bundle bundle = getArguments();
    investmentType = bundle.getString("TYPE");


    final TableLayout stk = (TableLayout) getActivity().findViewById(R.id.table_main);

    TableRow tbrow = new TableRow(getActivity());

    TextView textView1 = (TextView) getActivity().findViewById(R.id.MonthName);
    tbrow.addView(getTextView(getMonthName(0), textView1.getMeasuredWidthAndState(), 0.5f,0));


    TextView textView2 = (TextView) getActivity().findViewById(R.id.Opening);
    tbrow.addView(getTextView(formatValue(Double.parseDouble(principal)), textView2.getMeasuredWidthAndState(), 0.90f,0));

    String initialMaturityValue = calculateMaturity(principal);
    String initialInterestEarned = interestEarned(initialMaturityValue, principal);

    TextView textView3 = (TextView) getActivity().findViewById(R.id.InterestE);
    tbrow.addView(getTextView(initialInterestEarned, textView3.getMeasuredWidthAndState(), 0.70f,0));

    TextView textView4 = (TextView) getActivity().findViewById(R.id.closing);
    tbrow.addView(getTextView(initialMaturityValue, textView4.getMeasuredWidthAndState(), 0.90f,0));

    stk.addView(tbrow);

    String LoopMaturityValue = "";
    for (int i = 1; i < duration; i++) {
        tbrow = new TableRow(getActivity());


        LoopMaturityValue = calculateMaturity(initialMaturityValue);

        textView1 = (TextView) getActivity().findViewById(R.id.MonthName);
        tbrow.addView(getTextView(getMonthName(i), textView1.getMeasuredWidthAndState(), 0.5f,i));


        textView2 = (TextView) getActivity().findViewById(R.id.Opening);
        tbrow.addView(getTextView(initialMaturityValue, textView2.getMeasuredWidthAndState(), 0.90f,i));

        textView3 = (TextView) getActivity().findViewById(R.id.InterestE);
        tbrow.addView(getTextView(interestEarned(LoopMaturityValue, initialMaturityValue), textView3.getMeasuredWidthAndState(), 0.70f,i));

        textView4 = (TextView) getActivity().findViewById(R.id.closing);
        tbrow.addView(getTextView(LoopMaturityValue, textView4.getMeasuredWidthAndState(), 0.90f,i));

        initialMaturityValue = LoopMaturityValue;

        stk.addView(tbrow);
    }

}

private TextView getTextView(String value, int width, float weight,int i) {



    textView = new TextView(getActivity());
    textView.setText(value);
  //  textView.setTextColor(getResources().getColor(R.color.Black));

    TableRow.LayoutParams params = new TableRow.LayoutParams(width, (int) dipToPixels(getActivity(),40), weight);
    textView.setLayoutParams(params);
    textView.setGravity(Gravity.CENTER);
    textView.setPadding(3,0,3,0);

    if (i % 2 == 0)
        textView.setBackground(getActivity().getResources().getDrawable(R.drawable.back));

    else
        textView.setBackground(getActivity().getResources().getDrawable(R.drawable.border));



    return textView;

}

1 个答案:

答案 0 :(得分:-1)

因此请尝试删除padding_top并查看。希望它有所帮助