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

时间:2012-04-17 17:23:33

标签: android layout resize margin

我正在以编程方式更改framelayout内部布局的边距。我的目标是制作像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的大小。我希望你能理解我想要的东西:)。

由于

3 个答案:

答案 0 :(得分:2)

为了实现类似的功能,我们扩展了一个Horizo​​ntalScrollView - 只需重写onInterceptTouchEvent和onTouchEvent就可以返回false。 然后你只需要将菜单放在左侧,将内容放在右侧(内容必须与屏幕宽度相匹配)。

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

我希望这会有所帮助。

答案 1 :(得分:1)

以下是使用TranslateAnimation并为动画设置侦听器的示例代码,以及

   @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);

    }

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

答案 2 :(得分:0)

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

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

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