【发布时间】:2012-04-11 23:03:36
【问题描述】:
如图所示。我想在 layout 上方显示星图。 我是这种编码的新手。
我的查询是 1)是否可以在我的布局侧显示星(图像)?
2)如果每次点击我的活动时,我都想显示星星并显示第二张图片,这可能吗? 意味着在同一个屏幕上我必须用他们的尊重位置来保存星星。
感谢高级?
【问题讨论】:
标签: android image android-layout layout
如图所示。我想在 layout 上方显示星图。 我是这种编码的新手。
我的查询是 1)是否可以在我的布局侧显示星(图像)?
2)如果每次点击我的活动时,我都想显示星星并显示第二张图片,这可能吗? 意味着在同一个屏幕上我必须用他们的尊重位置来保存星星。
感谢高级?
【问题讨论】:
标签: android image android-layout layout
类似这样的:
public class MyActivity extends Activity implements View.OnTouchListener {
private RelativeLayout _mainLayout;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_mainLayout = (RelativeLayout)findViewById(R.id.canvas);
_mainLayout.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ImageView image = new ImageView(this);
image.setImageBitmap(bitmap);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
layoutParams.leftMargin = X - bitmap.getWidth()/2;
layoutParams.topMargin = Y - bitmap.getHeight()/2;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
image.setLayoutParams(layoutParams);
_mainLayout.addView(image);
break;
}
return false;
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/canvas"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg0large"
>
</RelativeLayout>
【讨论】:
要在布局上做布局,你应该使用RelativeLayout。
【讨论】:
如果你想在另一个布局上显示一个布局,你应该使用 FrameLayout http://developer.android.com/resources/articles/layout-tricks-merge.html
【讨论】: