Why Does Linearlayout Instance.getlayoutparams Look To Have A Wrong Class?
Solution 1:
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()
.
Solution 2:
Just to put it in a very straight way:
Please keep in mind that when you get the LayoutParams of a view, their class is determined by the class of the parent view of this view.
This implies one very important fact:
You have to cast the LayoutParams of a View because you can not know beforehand what is the class of the parent. So, if view's parent is a LinearLayout, it's LayoutParams will be of type LinearLayout.LayoutParams and STILL you have to cast it to this type!
I am not sure of the source code and how it goes. But it is very logical since RelativeLayout.LayoutParams, for example has properties such as ABOVE and BELOW which are not found in a LinearLayout.LayoutParams. And these params are actually dependent on the parent.
Solution 3:
You don't cast LinearLayout.LayoutParams to LinearLayout.LayoutParams. That makes no sense. Also it's not clear if linearLayout
in your code is an instance variable... Either way, you can simply reference LayoutParams inside of a View that extends from some type of View to get that View's LayoutParams. For example, if you're inside of a class that extends from RelativeLayout, then
LayoutParams params = new LayoutParams(200, LayoutParams.WRAP_CONTENT);
gets you RelativeLayout.LayoutParams
. Otherwise, you'll need to qualify the LayoutParams
you're looking for. To get/apply LinearLayout LayoutParams, you simply need
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams();
EDIT
linearLayout.getLayoutParams()
returns the layoutParams
that you supplied when you called setLayoutParams
, so first make sure you do that. Then once you've done that, you simply need to cast the LayoutParams
that were returned to the LayoutParams
you set it to. so
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams()
Also, why do you need to get the params like this? What are you looking to accomplish ultimately? I ask because there may be an easier way to do what you're trying to do...
In effect, the answer to your question is YES, you need to cast the LayoutParams you get from getLayoutParams because that's a method derived from the View class which returns the superclass of LayoutParams which is ViewGroup.LayoutParams which, because of polymorphism, can be casted to the type you know it to be.
Solution 4:
Are you using Eclipse and just using the Eclipse 'Organise Imports' option to import Android libraries? If so, there's a few different libraries called LayourParams, Eclipse seems to be importing the framelayout one and not the linearlayout one like you need.
So just change the imports at the top of your class.
Solution 5:
You don't need to get the LinearLayout Params object from the LinearLayout itself. You can create a new one by yourself.
// Set up the height and width of the layout, you can set FILL_PARENT // or even WRAP_CONTENT
LinearLayout.LayoutParamslp=newLinearLayout.LayoutParams(width, height);
// Set up your margins
lp.setMargins(left, top, right, bottom);
Now use this lp
object on your LinearLayout object instance
linearLayout.setLayoutParams(lp);
Post a Comment for "Why Does Linearlayout Instance.getlayoutparams Look To Have A Wrong Class?"