繁体   English   中英

Android leftMargin不会移动,但会扩展视图。 我该如何移动?

[英]Android leftMargin is not moving, but expanding view. How can I move it?

我在线性水平视图中有4张图像。 我使用XML使得第一张图片在开始时就适合屏幕宽度。 我有向左/向右箭头,所以当我点击向右箭头时,我想转换水平布局以查看上一张/下一张图片。

我的想法是更改整个布局的边距,以使其移动。 但是,当我更改左边距时,它只是将第一张图像扩展到屏幕外部,即左侧。 为什么会发生这种情况,我该如何翻译整个内容而不能得到这种扩大的效果?

下面是我的XML和Java:

  <LinearLayout android:id="@+id/HomeGallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
                <LinearLayout android:id="@+id/innerHomeGallery"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >
                <ImageButton android:id="@+id/imageButtonHomeGallery1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/topimg12x"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:background="@null" /> 
         <ImageButton android:id="@+id/imageButtonHomeGallery2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/topimg22x"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:background="@null" />
        <ImageButton android:id="@+id/imageButtonHomeGallery3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/topimg32x"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:background="@null" />
        <ImageButton android:id="@+id/imageButtonHomeGallery4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/topimg42x"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:background="@null" />
                </LinearLayout> 
            </LinearLayout>

..........

LinearLayout innerHomeGallery  = (LinearLayout)findViewById(R.id.innerHomeGallery);

    android.widget.LinearLayout.LayoutParams contentParams = (LinearLayout.LayoutParams)innerHomeGallery.getLayoutParams();

    contentParams.leftMargin=-600;

    innerHomeGallery.setLayoutParams(contentParams);

如果您要实现一种设计,使用户能够通过左右箭头键在图像之间导航,请尝试使用ViewPager,或者如果需要更基本的方法,请按照此步骤操作。

  1. 保持框架布局为根视图,并添加第一个孩子想要的任意尺寸的Imageview

  2. 您可以在框架布局中再添加两个子控件作为左右导航按钮。

现在为两个按钮都实现onClick侦听器,现在只需添加代码即可在相应的按钮单击时为ImageView设置适当的图像。 您还可以在动画中添加不同的动画,例如淡入淡出或淡入淡出,以使其看起来更酷。

希望能帮助到你。

暂无
暂无

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

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