2015-06-02 49 views
0

我試圖在我的Android應用程序上顯示一張圖像(一條蛇),該圖像可能會在上出現電路板表(這是一個tablelayout)。我怎麼能這樣做,而不是簡單地顯示圖像以上的表如何在Android中的桌面上顯示圖像

enter image description here

package com.example.test; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    TableLayout table; 
    ImageView image; 
    private static final int TABLE_WIDTH = 12; 
    private static final int TABLE_HEIGHT = 10; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     table = (TableLayout) findViewById(R.id.view_root); 
     image = (ImageView) findViewById(R.id.imageView1); 

     // Populate the table with stuff 
     for (int y = 0; y < TABLE_HEIGHT; y++) { 
      final int row = y; 
      TableRow r = new TableRow(this); 
      table.addView(r); 

      for (int x = 0; x < TABLE_WIDTH; x++) { 
       final int col = x; 
       Button b = new Button(this); 
       b.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
//      Toast t = Toast.makeText(getApplicationContext(), "You clicked (" + row + "," + col + ")", Toast.LENGTH_SHORT); 
         Toast t = Toast.makeText(getApplicationContext(), "Tooth cavity in 6 month\n (previously in 2 years)", Toast.LENGTH_SHORT); 
         t.getView().setBackgroundColor(Color.RED); 
         t.show(); 
         if(row==0 && col==0){ 
          image.setImageResource(R.drawable.snake); 
         } 
         else{ 
          image.setVisibility(View.GONE); 
         } 
        } 

       }); 
       r.addView(b); 
      } 
     } 
    } 
} 

而且佈局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/view_root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:shrinkColumns="*" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</TableLayout> 

回答

0

您可以創建一個FrameLayoutTableLayout並在它的ImageView。由於FrameLayouts具有重疊事物的能力,因此使得imageview的背景透明,並在其上畫蛇。

相關問題