ImageView,如何设置图像看起来在另一个图像的前面?
答
可以使用的RelativeLayout:
RelativeLayout rv = (RelativeLayout) findViewById(R.id.my_ph);
RelativeLayout.LayoutParams params;
ImageButton im1 = new ImageButton(this);
im1.setBackgroundResource(R.drawable.lamp);
im1.setId(i);
im1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("lamp #" + v.getId());
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = x;
params.topMargin = y;
rv.addView(im1, params);
XML布局:
<RelativeLayout
android:id="@+id/my_ph"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/map" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="@+id/image"
android:layout_alignParentLeft="true">
</TextView>
</RelativeLayout>
答
如果要创建通过代码的图像,你可以使用的LayoutParams给第二图像负LEFTMARGIN。
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageParams.leftMargin = -20; // change this value
imageParams.addRule(RelativeLayout.RIGHT_OF,image1.getId());
image2.setLayoutParams(imageParams);
这将强制第一个图像上的第二个图像。通过更改左边距,您可以决定第二张图像在第一张图像上的移动量。
您可以为此使用框架布局。第2张图像将显示在第1张的顶部。 – sUndeep 2015-02-10 09:54:05