繁体   English   中英

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

[英]Avoid layout to be resized on margin changed

我正在以编程方式更改框架布局内的布局边距。 我的目标是制作一个像 Facebook 应用程序一样的滑动视图。 是否可以避免调整此布局的大小? 这是我的布局移动:

<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>

我不希望调整左侧 LinearLayout 的大小。 我希望你会明白我想要什么:)

谢谢

为了实现类似的效果,我们扩展了 Horizo​​ntalScrollView - 只是覆盖 onInterceptTouchEvent 和 onTouchEvent 以始终​​返回 false。 然后你只需要把你的菜单放在左边,内容放在右边(内容必须与屏幕宽度匹配)。

最后设置初始 Horizo​​ntalScrollView 滚动并绑定到按钮单击事件以更改滚动位置(horizo​​ntalScrollView.scrollTo 或 horizo​​ntalScrollView.smoothScrollTo)。

我希望这有帮助。

以下是使用 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);

    }

我希望它能让你们清楚如何在动画后移动你的视图

在过去的几个小时里,我也一直在为此挠头。 事实证明,如果您更改两个相反的边距,则 ReleativeLayout 内的视图将不会调整大小或四处移动:

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

这很疯狂,但它有效......

暂无
暂无

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

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