Pergunta

Hy i'm having a problem. I want to change the height of a relative layout in code. I currently do this in onCreateView with the following method.

The problem is when I change the action bar tab (from first to the third tab and then back to the first) the XML height overrides this one in code, it jumps to 40dp. But if i change the activity and then come back the view is the correct size as specified in the code, in other words the onCreateView is called correctly.

I can't seem to figure it out why it does that. Thanks for any help.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    Bundle args = getArguments();
    rootView.setOnClickListener(clickListener);

    home_details_container_layout = (RelativeLayout) rootView.findViewById(R.id.view_home_details_container);
    home_details_layout = (RelativeLayout) rootView.findViewById(R.id.view_home_details);

    ViewTreeObserver vto = home_details_container_layout.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            home_details_container_layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

            home_details_container_width  = home_details_container_layout.getMeasuredWidth();
            home_details_container_height = home_details_container_layout.getMeasuredHeight();

            home_details_height = home_details_container_height*height_factor;
            home_details_layout.getLayoutParams().height = (int) (home_details_height);
        } 
    });

    return rootView;
}

Here is the XML:

<RelativeLayout
        android:id="@+id/view_home_details_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/view_home_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/gray_dark">

            <com.custom.views.CustomView
                android:id="@+id/view_id"
                android:layout_width="match_parent"
                android:layout_height="40dp"

            />
        </RelativeLayout>


    </RelativeLayout>
Foi útil?

Solução

Oh my gosh i can't believe i spent a whole day yesterday figuring this out. I was supposed to set the

mViewPager.setOffscreenPageLimit(numOfFragments);

In the main activity where i hold the viewPager. Therefore the fragments are kept in memory for some time i guess and the layouts don't lose their size adjustment in code or any other interaction of that sort. I just wanted to point out that you should pay attention to details like this and use this line of code any time you are implementing an action bar with tab changing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top