Reputation: 1868
I'm seeing this weird behavior and would like to ask if anyone found a workaround. I have a simple RelativeLayout with another layout inside.
The weird behavior happens in the code. If I get LayoutParams from the nested layout, and set it back to it, it changes the way layout looks. It seems like some of the parameters are lost in the process. Here is the code:
RelativeLayout v = (RelativeLayout)findViewById(R.id.group1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(v.getLayoutParams());
v.setLayoutParams(lp);
After this call I'm expecting everything to remain as it was, however, I'm losing "layout_AlignParentRight" effect (at least). Can anyone help me understand why this happens?
And from a practical standpoint, all I need this for is to change the width of the group1 layout at runtime. So I'm doing it via changing the width on "lp" above, and it works, but messes up the alignment. So if there are any suggestions on alternative approaches for changing the size of layout without impacting other properties, please chime in too.
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/group1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true">
<TextView
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test">
</TextView>
</RelativeLayout>
</RelativeLayout>
Upvotes: 2
Views: 2316
Reputation: 134664
Just modify the params that already exist rather than instantiating new ones:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)v.getLayoutParams();
params.width = myNewWidth;
v.requestLayout();
Upvotes: 7