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

6

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

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

但是没有成功。

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


我在AbsoluteLayout中使用的TextView <AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > - Ranjitsingh Chandel
3个回答

10
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 实例吗? - BertKing

5
我调用new TableRow(),因为textView的父视图是TableRow。如果你的父视图是LinearLayout,请用new LinearLayout.LayoutParams替换它。
textView.setLayoutParams(
    new TableRow.LayoutParams(
        LayoutParams.WRAP_CONTENT, 
        LayoutParams.MATCH_PARENT));

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接