问题
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