简体   繁体   English

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

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

I am trying to set TextView width dynamically, Using setWidth(width) method. 我试图动态设置TextView宽度,使用setWidth(width)方法。

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

But not success. 但不成功。

Please help me How can I set width of textview dynamically. 请帮帮我如何动态设置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);  

I call new TableRow() , because textView's parent is TableRow . 我调用了new TableRow() ,因为textView的父级是TableRow If your parent's view is LinearLayout , replace it by new LinearLayout.LayoutParams . 如果您的父视图是LinearLayout ,请将其替换为new LinearLayout.LayoutParams

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

I have experienced something similar in situations where there is a ScrollView in the parents hierarchy. 在父级层次结构中存在ScrollView的情况下,我遇到了类似的情况。 No matter how I would modify layout inside it, my commands were ignored. 无论我如何修改其中的布局,我的命令都会被忽略。 If this is the case, try calling RequestLayout on the ScrollView or remove and re-add the child view of the ScrollView. 如果是这种情况,请尝试在ScrollView上调用RequestLayout,或者删除并重新添加ScrollView的子视图。

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

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