Is it ok to use LinearLayout.LayoutParams in case I don't need advanced functionality?

45 Views Asked by At

I've noticed that some Android views come with a custom version of LayoutParams, e.g. when adding an ActionBar to a LinearLayout I could set its dimensions like this:

android.support.v7.app.ActionBar.LayoutParams params = new android.support.v7.app.ActionBar.LayoutParams(android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT, actionBarSize);
actionBar.setLayoutParams(params);  

But as you can see, this is extremely wordy. So I tried using LinearLayout.LayoutParams instead, which makes it much shorter and better readable, like so:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, actionBarSize);
actionBar.setLayoutParams(params);

I've tried both versions and they seem to do exactly the same.

That's what makes me wonder whether it's ok to just go with LinearLayout.LayoutParams in case I don't need any advanced functionality provided by custom versions such as android.support.v7.app.ActionBar.LayoutParams?

Or does using LinearLayout.LayoutParams for ActionBar (and similar cases) have any potential unwanted side effects?

1

There are 1 best solutions below

0
dgngulcan On BEST ANSWER

Both LinearLayout.LayoutParams and ActionBar.LayoutParams inherit from ViewGroup.LayoutParams. Thus, they both behave the same for width and height attribute.

For reference: https://developer.android.com/reference/android/view/ViewGroup.LayoutParams