2012-03-28 164 views
1

enter image description hereAndroid layoutdisplay,佈局佈局。

如顯示在圖片上。我想在佈局上方顯示星形圖像。 我是這種類型的編碼的新手。

我的查詢是 1)將有可能在我的佈局一側顯示星號(圖片)?

2)如果每次點擊我的活動,我想要顯示星形,並顯示像第二個圖像可能嗎? 在同一屏幕上的手段我必須保存與他們的尊重位置的明星。

enter image description here Thanks in Advanced?

回答

0

要做佈局佈局,你應該使用RelativeLayout。

+2

它應該是評論而不是回答 – Krish 2012-03-28 11:10:11

1

事情是這樣的:

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> 

+0

緊急你,,, 可能是我的工作.. 我有一個疑問。我會在另一張圖像上顯示一張圖像嗎? – 2012-03-28 12:18:07

+0

是的,試試.... – 2012-03-28 12:22:30