繁体   English   中英

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

[英]Dynamically set width and height of TextView in Android

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

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

但不成功。

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

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

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

我调用了new TableRow() ,因为textView的父级是TableRow 如果您的父视图是LinearLayout ,请将其替换为new LinearLayout.LayoutParams

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM