简体   繁体   中英

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

I have 4 images in a linear horizontal view. Using the XML I have made it that the first image will fit to width of screen at start. I have left / right arrows, so when I tap on right arrow I would like to translate the horizontal layout to see previous / next picture.

My idea was to change the margin of the entire layout so that it moves. However, when I change the left margin, it just expands the first image to outside the screen, to the left. Why is this happening, how can I translate the entire thing and not get this expanding effect?

Below, my XML and my 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);

If you are trying to implement a design where user will be able to navigate between images through arrow keys left and right, then try using ViewPager, or for more basic follow this.

  1. Keep frame layout as your root view, add Imageview of whatever dimension you want as first child

  2. You can add two more child as left and right navigation button to the framelayout.

now implement onClick listeners for both of the button, now just add code for setting proper image to ImageView on respective button clicks. You can also add in animations different animations like fade in or our to make it look cooler.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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