问题
I am using relative layout to superimpose one smaller image on top of a larger one. I want the bottom-right corner of the smaller image to coincide with B-R corner of the larger image. I'm using margin parameters in my layout XML (specifying measurement in dips) but this doesn't seem to work for all devices and resolutions - in some cases the small image is shifted by 4-5px from the border.
Is it possible to specify the position of the smaller image without pixel values? I.e. with gravity or something?
回答1:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/big_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/bigimage"/>
<ImageView
android:id="@+id/little_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignRight="@id/big_image"
android:layout_alignBottom="@id/big_image"
android:src="@drawable/littleimage"/>
</RelativeLayout>
Nice thing about RelativeLayout
is that you can use relative positions and dimensions, instead of using specific dips or pixels. In the case above, I just played with the android:layout_align*
parameters, to make sure that both images are aligned. Keep in mind that I set a specific dimension of the little image, but you can change that to fit your needs.
来源:https://stackoverflow.com/questions/4079410/aligning-images-about-right-bottom-corner