android表行需要以行的百分比换行

时间:2016-12-16 22:50:22

标签: android tablelayout

我有以下代码,我需要每个表行的第二行以屏幕宽度的百分比换行...比如说80%。 其textview3字段需要换行,同时在屏幕上保留row1

应该是

RowOneLeft RowOneRight

这一行应该再次包装\ n

一次又一次

一次又一次。

  //Layout
        RelativeLayout global_relativelayout = new RelativeLayout(getActivity());
        //atulsLayout.setBackgroundColor(Color.GREEN);

    RelativeLayout.LayoutParams tableDetails= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams tablerowDetails= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams linearvparam= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams linearh1param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams linearh2param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams linearh3param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams textview1param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textview2param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textview3param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams textview4param= new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    TableLayout maintable = new TableLayout (getActivity());
    maintable.setStretchAllColumns(true);
    maintable.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    maintable.setWeightSum(1);

    ScrollView vscroll = new ScrollView(getActivity());
    vscroll.setFillViewport(true);

    maintable.removeAllViews();
    TableRow tablerow0 = new TableRow(getActivity());
    TextView textviewl0 = new TextView(getActivity());
    textviewl0.setText("TITLE ");
    //maintable.addView(tablerow0);

    LayoutParams tablerowparam10 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
    LayoutParams tablerowparam05 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f);
    for (int i = 0; i < 125; i++) {

        final TableRow tablerow = new TableRow(getActivity());

        tablerow.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT, 1.0f));

        final LinearLayout linearv = new LinearLayout(getActivity());
        linearv.setOrientation(LinearLayout.VERTICAL);
        final LinearLayout linearh1 = new LinearLayout(getActivity());
        linearh1.setOrientation(LinearLayout.HORIZONTAL);
        final LinearLayout linearh2 = new LinearLayout(getActivity());
        linearh2.setOrientation(LinearLayout.HORIZONTAL);

        final LinearLayout linearh3 = new LinearLayout(getActivity());
        linearh3.setOrientation(LinearLayout.HORIZONTAL);
        linearh3.setBackgroundColor(Color.BLUE);
        linearh3param.height = 10;
        //linearh3param.width = 600;
        linearh3.setGravity(Gravity.CENTER_HORIZONTAL);
        linearh3.setLayoutParams(linearh3param);

        //Row1
        TextView textview1 = new TextView(getActivity());
        textview1.setId(id_textview1);
        textview1.setText("RowOneLeft " + i);
        textview1.setTextColor(Color.BLACK);
        textview1.setGravity(Gravity.LEFT);
        textview1param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textview1.setLayoutParams(textview1param);
        linearh1.addView(textview1,tablerowparam05);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("RowOneRight " + i);
        textview2.setTextColor(Color.BLACK);
        textview2.setGravity(Gravity.RIGHT);
        textview2param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        textview2.setLayoutParams(textview2param);
        linearh1.addView(textview2,tablerowparam10);

        //Row2
        TextView textview3 = new TextView(getActivity());
        textview3.setText("This Needs To Wrap  This Needs To Wrap This Needs To WrapEnd " + i);
        //textview3.setText("RowtwoRight  End " + i);
        textview3.setTextColor(Color.BLACK);
        textview3.setGravity(Gravity.LEFT);
        textview3.setSingleLine(false);
        textview3param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textview3param.addRule(RelativeLayout.LEFT_OF,textview1.getId());
        textview3.setLayoutParams(textview3param);
        linearh2.addView(textview3,tablerowparam05);

        tablerow.setId(i);

        linearv.addView(linearh1,linearh1param);
        linearv.addView(linearh2,linearh2param);
        linearv.addView(linearh3,linearh3param);

        tablerow.addView(linearv);
        maintable.addView(tablerow);
    }

    vscroll.addView(maintable,tableDetails);
    view = vscroll;

    return view;
}

Screen Print

1 个答案:

答案 0 :(得分:0)

检查PercentRelativeLayout

您可以在xml中设置百分比宽度:

  app:layout_widthPercent="80%"

或代码:

PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.widthPercent = 80;