Why doesn't the gravity attract things that float on water????
When you put something in water, gravity can pull the object down through the water only if an equal volume of water is allowed to go up against the force of gravity; this is called displacement. In effect gravity has to choose which it will pull down, the water or the immersed object. What we call buoyancy is, in effect, forcing gravity to make this choice.
Faced with this choice, gravity will act more strongly on whichever has more mass (thus, more weight) per given volume. So if the thing you immerse is denser than water it will sink, but its apparent weight is reduced by the volume of water that gets displaced upward. If instead the water is denser, the immersed object will float up to the point where the displaced volume of water matches the whole object's mass. Then the net weight is zero.