Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

java - Why does LinearLayout instance.getLayoutParams look to have a wrong class?

If I declare LinearLayout linearLayout and look at linearLayout.getLayoutParams(), it gives me ViewGroup.LayoutParams, not LinearLayout.LayoutParams.

So I have to use the repeating (and thus bad) style construction of:

int lm = ((LinearLayout.LayoutParams) linearLayout.getLayoutParams()).leftMargin?

Do I really have to use it, if I want to reach margins, for example?

Is it my misunderstanding of Android or Java, or both or something else?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think your understanding about 'LayoutParams' is incorrect. A view's (or layout's) LayoutParams must be an instance of 'the parent view's LayoutParams'.

For example, here's an LinearLayout in RelativeLayout. That LinearLayout's LayoutParams must be RelativeLayout.LayoutParams. This thing enables us to set the attributes properly, like 'centerInParent' and etc.

When you call getLayoutParams(), that returns various instance of LayoutParams that matches to parent ViewGroup type. As a result, we need to cast the type of a view's LayoutParams repeatedly when calling getLayoutParams().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...