简体   繁体   English

避免在更改边距时调整布局

[英]Avoid layout to be resized on margin changed

I'm programmatically changing the margin of a layout inside a framelayout.我正在以编程方式更改框架布局内的布局边距。 My goal is to make a sliding view like the Facebook app.我的目标是制作一个像 Facebook 应用程序一样的滑动视图。 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.我不希望调整左侧 LinearLayout 的大小。 I hope you will understand what I want :)我希望你会明白我想要什么:)

Thanks谢谢

To achieve something similar we extended a HorizontalScrollView - just overriding onInterceptTouchEvent and onTouchEvent to always return false.为了实现类似的效果,我们扩展了 Horizo​​ntalScrollView - 只是覆盖 onInterceptTouchEvent 和 onTouchEvent 以始终​​返回 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).最后设置初始 Horizo​​ntalScrollView 滚动并绑定到按钮单击事件以更改滚动位置(horizo​​ntalScrollView.scrollTo 或 horizo​​ntalScrollView.smoothScrollTo)。

I hope this helps.我希望这有帮助。

The below is an example code for using TranslateAnimation and set listener for the animation and in以下是使用 TranslateAnimation 并为动画设置侦听器的示例代码,并在

   @Override
    public void onAnimationEnd(Animation animation) 
    {

    //Just set the layout params for your view (layout) which is animated,
    //to make your 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我希望它能让你们清楚如何在动画后移动你的视图

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:事实证明,如果您更改两个相反的边距,则 ReleativeLayout 内的视图将不会调整大小或四处移动:

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...这很疯狂,但它有效......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM