【问题标题】:Avoid layout to be resized on margin changed避免在更改边距时调整布局大小
【发布时间】:2012-04-29 01:46:48
【问题描述】:

我正在以编程方式更改框架布局内布局的边距。我的目标是制作像 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 的大小。我希望你能明白我想要什么:)

谢谢

【问题讨论】:

  • 您找到解决方案了吗?!这正是我现在所面临的。
  • 也许用 setX 而不是 margin 的绝对布局来解决问题

标签: android layout resize margin


【解决方案1】:

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

    }

我希望它能让大家清楚如何在动画后移动视图

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      我希望这会有所帮助。

      【讨论】:

      • 感谢 Juliano 但这我也有其他动画的问题。事实上,使用自定义动画,例如包含 ImageView,ImageView 在翻译过程中被缩放。你知道 Android 在使用 TranslateAnimation 时是如何执行动画的吗? TranslateAnimation 在动画期间不缩放 le 视图...
      猜你喜欢
      • 2020-12-12
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多