【问题标题】:android table row need to wrap at percentage of rowandroid表行需要以行的百分比换行
【发布时间】:2016-12-20 21:37:25
【问题描述】:

我有以下代码,我需要每个表格行的第二行以屏幕宽度的百分比换行……比如 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;
}

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    检查PercentRelativeLayout

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

      app:layout_widthPercent="80%"
    

    或在代码中:

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

    【讨论】:

      猜你喜欢
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      相关资源
      最近更新 更多