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

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...