【问题标题】:Dynamically set width and height of TextView in AndroidAndroid中动态设置TextView的宽高
【发布时间】:2012-09-29 19:34:29
【问题描述】:

我正在尝试动态设置 TextView 宽度,使用 setWidth(width) 方法。

txtviewOne.setWidth(10);
txtviewTwo.setWidth(10);

但没有成功。

请帮助我如何动态设置 textview 的宽度。

【问题讨论】:

  • 我在AbsoluteLayout中使用的TextView

标签: height width textview android


【解决方案1】:
TextView tv;
----------------------
tv=new TextView(this);   // or  tv=new TextView(R.id.textview1);

LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
tv.setLayoutParams(Params1);  

【讨论】:

  • 你当然可以使用 resId 来创建一个 TextView 实例吗?
【解决方案2】:

我调用new TableRow(),因为textView 的父级是TableRow。如果您父母的观点是LinearLayout,请将其替换为new LinearLayout.LayoutParams

textView.setLayoutParams(
    new TableRow.LayoutParams(
        LayoutParams.WRAP_CONTENT, 
        LayoutParams.MATCH_PARENT));

【讨论】:

    【解决方案3】:

    在父层次结构中有 ScrollView 的情况下,我也遇到过类似的情况。无论我如何修改其中的布局,我的命令都被忽略了。如果是这种情况,请尝试在 ScrollView 上调用 RequestLayout 或删除并重新添加 ScrollView 的子视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2012-08-21
      • 2011-04-06
      相关资源
      最近更新 更多