Avoid layout to be resized on margin changed

2019-07-10 11:13发布

问题:

I'm programmatically changing the margin of a layout inside a framelayout. My goal is to make a sliding view like the Facebook app. Is it possible to avoid this layout to be resized? This is my layout moving:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#00a2ff"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivGPSSearching"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:src="@drawable/actionbar_gps_searching" />
    </LinearLayout>

    <ImageView
        android:id="@+id/ivStarsFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/actionbar_star" />
</LinearLayout>

I don't want the left LinearLayout to be resized. I hope you will understand what I want :)

Thanks

回答1:

To achieve something similar we extended a HorizontalScrollView - just overriding onInterceptTouchEvent and onTouchEvent to always return false. Then you just need to put your menu in the left side and the content on the right (the content must match the screen width).

Finally setup the initial HorizontalScrollView scroll and bind to a button click a event to change the scroll position(horizontalScrollView.scrollTo or horizontalScrollView.smoothScrollTo).

I hope this helps.



回答2:

The below is an example code for using TranslateAnimation and set listener for the animation and in

   @Override
    public void onAnimationEnd(Animation animation) 
    {

    //Just set the layout params for your view (layout) which is animated,
    //to make yout view shift to  the new position

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutWidth, LayoutHeight);

     params.leftMargin = 100; //for example you want to shift 100 px from left

     params.rightMargin = -Layoutwidth; //this will avoid your view 
     //from shrinking and make it as wide as its width was, and - negative value will 
     //move it towards right

    otherLayout.setLayoutParams(params);

    }

I hope it make you people clear how to move your view after animation



回答3:

I've been scratching my head around this for the last few hours as well. Turns out that if you change both opposite margins, the views inside the ReleativeLayout will not be resized nor moved around:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(0, - yOffset, Integer.MIN_VALUE, yOffset);

This is crazy but it works...