在Android中动态设置TextView的宽度和高度

时间:2012-10-09 12:57:14

标签: height width textview android

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

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

但不成功。

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

3 个答案:

答案 0 :(得分:8)

TextView tv;
----------------------
tv=new TextView(this);   // or  tv=new TextView(R.id.textview1);

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

答案 1 :(得分:3)

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

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

答案 2 :(得分:0)

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

相关问题