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?
I think your understanding about ‘LayoutParams’ is incorrect. A view’s (or layout’s)
LayoutParamsmust 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 callinggetLayoutParams().