How do I get the `layout_weight` of a TextView?

血红的双手。 提交于 2019-12-23 06:02:28

问题


I have a TableLayout and a row with a couple TextViews. I want to find the layout_weight of the TextView but I cant figure out how. I've tried the following:

TableRow.LayoutParams lp = tv1.getLayoutParams();

and:

ViewGroup.LayoutParams lp = tv1.getLayoutParams();

In the first case I get a type mismatch: "Cannot convert from ViewGroup.LayoutParams to TableRow.LayoutParams", and the second case works fine, however ViewGroup.LayoutParams doesn't have a weight field.

All the different types of LayoutParams are confusing, I'm never sure which to use where.


回答1:


Try this

TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

Try this link




回答2:


You almost had it with your first try. You just need to cast to the correct type:

TableRow.LayoutParams lp = (TableRow.LayoutParams) tv1.getLayoutParams();

Any subclass of LinearLayout.LayoutParams (including TableRow.LayoutParams) will have a weight field.



来源:https://stackoverflow.com/questions/14334391/how-do-i-get-the-layout-weight-of-a-textview

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