Dynamically set width and height of TextView in Android

若如初见. 提交于 2020-01-14 08:46:33

问题


I am trying to set TextView width dynamically, Using setWidth(width) method.

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

But not success.

Please help me How can I set width of textview dynamically.


回答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);  



回答2:


I call new TableRow(), because textView's parent is TableRow. If your parent's view is LinearLayout, replace it by new LinearLayout.LayoutParams.

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



回答3:


I have experienced something similar in situations where there is a ScrollView in the parents hierarchy. 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.



来源:https://stackoverflow.com/questions/12800561/dynamically-set-width-and-height-of-textview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!