0

I have this view

<ScrollView ...>
    <RelativeLayout ...>

I define the RelativeLayout, one that I want to manipulate in Java, like this

ViewGroup parent = (ViewGroup) rootView
                        .findViewById(R.id.rltv_layout);

I want to change the Relative's height, so I figured I'd use

RelativeLayout.LayoutParams lpp = (RelativeLayout.LayoutParams) parent.getLayoutParams();
lpp.height = lpp.height + someHeights;
parent.setLayoutParams(lp);

Then error happened, LogCat said

05-18 14:01:49.829: E/AndroidRuntime(12856): java.lang.ClassCastException: 
android.widget.FrameLayout$LayoutParams cannot be cast 
to android.widget.RelativeLayout$LayoutParams

So I changed it to

FrameLayout.LayoutParams lpp = (FrameLayout.LayoutParams) parent.getLayoutParams();

Then error happened, LogCat said

05-18 14:06:16.297: E/AndroidRuntime(13183): java.lang.ClassCastException: 
android.widget.RelativeLayout$LayoutParams cannot be cast 
to android.widget.FrameLayout$LayoutParams

So what is it that it wants?

Also, AFAIK, LayoutParams type depended on its parent's type, not the View itself. At one point I tried ScrollView.LayoutParams and still got same error.

Konayuki
  • 674
  • 1
  • 9
  • 21

1 Answers1

1

Since you're only using lpp.height, using android.view.ViewGroup.LayoutParams directly would suffice. No casting needed.

Like you said, the LayoutParams depend on the parent view. But ScrollView doesn't define its own subclass. That's why you find FrameLayout.LayoutParams.

For the second cast issue, you might have set the LayoutParams to RelativeLayout.LayoutParams with parent.setLayoutParams(lp).

tynn
  • 38,113
  • 8
  • 108
  • 143