我如何获得一个TextView的

我如何获得一个TextView的

本文介绍了我如何获得一个TextView的`layout_weight`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TableLayout 和行与一对夫妇 TextViews 。我想找到 layout_weight 的TextView 但我找不出如何。我已经试过如下:

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();

ViewGroup.LayoutParams lp = tv1.getLayoutParams();

在第一种情况下,我得到一个类型不匹配:不能从ViewGroup.LayoutParams到TableRow.LayoutParams转换,不过和第二种情况下工作正常, ViewGroup.LayoutParams 没有一个重量字段。

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.

所有不同类型混淆的的LayoutParams ,我无法知道它使用的地方。

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

推荐答案

您几乎你的第一次尝试了它。你只需要转换为正确的类型:

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

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

的任何子类 LinearLayout.LayoutParams (包括 TableRow.LayoutParams )将有一个体重字段。

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

这篇关于我如何获得一个TextView的`layout_weight`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:04