-1

我已经缩放位图以显示TextView但如何添加TableLayout行和列?

    tbl = (TableLayout)findViewById(R.id.TableLayout1);
tblrow = new TableRow(this);


public void bukagambar(){
    bit = Bitmap.createScaledBitmap(bi, lebar, lebar, true);
    int lebar_tile = bit.getWidth()/ukurangrid;
    for(short baris=0 ;baris<ukurangrid;baris++){
        for(short kolom=0 ;kolom<ukurangrid;kolom++){
            bitm= Bitmap.createBitmap(bit, kolom*lebar_tile, baris*lebar_tile, lebar_tile, lebar_tile);
            bimg.setImageBitmap(bitm);
            tblrow.addView(bimg);
        }
        tbl.addView(tblrow);
    }   
}

我逼近了,抱歉英语不好


logcat 这个??

05-17 16:45:47.239: E/Trace(5339): error opening trace file: No such file or directory (2)
05-17 16:45:47.649: D/dalvikvm(5339): GC_FOR_ALLOC freed 3137K, 58% free 3606K/8387K, paused 31ms, total 31ms
05-17 16:45:54.479: D/dalvikvm(5339): GC_FOR_ALLOC freed 607K, 46% free 4554K/8387K, paused 18ms, total 27ms
05-17 16:45:54.489: D/AndroidRuntime(5339): Shutting down VM
05-17 16:45:54.489: W/dalvikvm(5339): threadid=1: thread exiting with uncaught exception (group=0x40b22300)
05-17 16:45:54.509: E/AndroidRuntime(5339): FATAL EXCEPTION: main
05-17 16:45:54.509: E/AndroidRuntime(5339): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addView(ViewGroup.java:3249)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addView(ViewGroup.java:3194)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addView(ViewGroup.java:3170)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at skripsi.slidame4.Slidame4.bukagambar(Slidame4.java:126)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at skripsi.slidame4.Slidame4.onOptionsItemSelected(Slidame4.java:89)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:977)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:81)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:940)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AbsListView.performItemClick(AbsListView.java:1088)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2861)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AbsListView$1.run(AbsListView.java:3535)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.os.Handler.handleCallback(Handler.java:615)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.os.Looper.loop(Looper.java:137)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.app.ActivityThread.main(ActivityThread.java:4931)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at dalvik.system.NativeStart.main(Native Method)
05-17 16:45:56.509: I/Process(5339): Sending signal. PID: 5339 SIG: 9

Slidame4.java代码

public class Slidame4 extends Activity {

private Bitmap bit,bi,bitm;
private TableRow tblrow ;
private ImageView bimg;
private kotak hitam;
private TextView v ;
private TableLayout tbl;
public int ukurangrid;
private GameBoard coba;
private int lebar;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Resources r = getResources();
    bi = BitmapFactory.decodeResource(r, R.drawable.gambar2);
    tbl = (TableLayout)findViewById(R.id.TableLayout1);
    tblrow = new TableRow(this);
    v= new TextView(this);
    bimg = new ImageView(this);
    coba = new GameBoard();
    Display display = getWindowManager().getDefaultDisplay();
    lebar = (display.getWidth());       
    setContentView(R.layout.activity_slidame4);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.slidame4, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case R.id.hint:
        Toast.makeText(getApplicationContext(), ""+lebar, Toast.LENGTH_SHORT).show();
        break;
    case R.id.highscore:
        Toast.makeText(getApplicationContext(), "HIGH SCORE ", 3).show();
        break;
    case R.id.grid3:
        ukurangrid = 3;
        bukagambar();
        item.setChecked(true);
        break;
    case R.id.grid4:
        ukurangrid = 4;
        bukagambar();
        item.setChecked(true);
        break;
    case R.id.grid5:
        ukurangrid = 5;
        bukagambar();
        item.setChecked(true);
        break;

    default:
        return super.onOptionsItemSelected(item);

    }
        return false;

}

public void bukagambar(){
    tblrow.removeAllViews();
    bit = Bitmap.createScaledBitmap(bi, lebar, lebar, true);
    int lebar_tile = bit.getWidth()/ukurangrid;
    for(short baris=0 ;baris<ukurangrid;baris++){
        for(short kolom=0 ;kolom<ukurangrid;kolom++){
            bitm= Bitmap.createBitmap(bit, kolom*lebar_tile, baris*lebar_tile, lebar_tile, lebar_tile);
            bimg.setImageBitmap(bitm);
            tblrow.addView(bimg);
        }
    tbl.addView(tblrow);    
    }


}

我补充说removeallview,但仍然逼近,

4

1 回答 1

0

删除此行

bimg = new ImageView(this);

并将其添加到

int lebar_tile = bit.getWidth()/ukurangrid;
bimg = new ImageView(tblrow);

(您ImageView将被创建为表格行的子项。

于 2013-05-17T17:21:22.953 回答