【问题标题】:how to set weight of textview Dyanamicaly inside Tablerow in TableLayout?如何在 TableLayout 中的 Tablerow 内设置 textview Dynaamicaly 的权重?
【发布时间】:2016-09-12 15:13:21
【问题描述】:

我在表格行内有三个 textview,但该 textview 的位置未正确对齐,并且我在 tablelayout 的 tablerow 内动态添加 textview,我如何设置 textview 的权重? 请帮帮我

 TableLayout table= (TableLayout) findViewById(R.id.table);
        TableRow tableRow=new TableRow(this);
        TextView txt1=new TextView(this);
        txt1.setTextSize(20);
        TextView txt2=new TextView(this);
        txt2.setTextSize(20);
        TextView txt3=new TextView(this);
        txt3.setTextSize(20);
        txt1.setText("Name");
        txt2.setText("Quantity");
        txt3.setText("Unit");
        tableRow.addView(txt1);
        tableRow.addView(txt2);
        tableRow.addView(txt3);
        table.addView(tableRow);

【问题讨论】:

  • 粘贴你的代码
  • 听dipali ji,你试过什么?
  • 我用基本代码更新问题

标签: android android-studio textview


【解决方案1】:

你可以在下面设置weightSum

    TableRow.LayoutParams tableRowparam = new TableRow.LayoutParams();  tableRowparam.weight=0.50f; 
txt1.setLayoutParams(tableRowparam); 

在上面的代码中,我采用0.50f 表示TableRow 中有2 TextView。您可以根据自己的要求进行更改。

【讨论】:

  • @AkshayPadalkar 发布您有问题的错误日志。
  • TableRow.LayoutParams tableRowparam = new TableRow.LayoutParams(); tableRowparam.weight=0.50f; txt1.setLayoutParams(tableRowparam);
  • TableRow.LayoutParams tableRowparam = new TableRow.LayoutParams(); tableRowparam.weight=0.50f; txt1.setLayoutParams(tableRowparam);这段代码对我有用
  • @AkshayPadalkar 如果这对你有用,那么接受答案,这样它会帮助其他人。
  • 你能告诉我如果我有 2 个文本视图,那么我的体重是多少?我设置了 0.50f 和 1.0f 但它不会占据整行
【解决方案2】:

如果您必须为应该缩放的孩子设置重量。

1 表示默认 100% 缩放,如果您希望 3 个 textvie 均匀分布,则为每个设置 0.33 作为权重。

从下面的代码中获取所有文本视图的想法:

使用TableLayout.LayoutParams

TableRow.LayoutParams trlp = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 0.33f);

tvFirst.setLayoutParams(trlp);  //tvFirst is first textview 

tr.add(tvFirst);

谢谢

【讨论】:

  • @AkshayPadalkar,请检查更新的答案并导入正确的布局参数
  • TableRow.LayoutParams tableRowparam = new TableRow.LayoutParams(); tableRowparam.weight=0.50f; txt1.setLayoutParams(tableRowparam);
  • 你能告诉我如果我有 2 个文本视图,那么我的体重是多少?我设置了 0.50f 和 1.0f 但它不会占据整行
猜你喜欢
  • 2015-02-28
  • 1970-01-01
  • 2011-10-05
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
相关资源
最近更新 更多