0

我正在编写以下代码..但有些部分工作,有些不工作,我不知道为什么..

这些工作正常

    ScrollView deviceList = (ScrollView) findViewById(R.id.deviceManager);
    deviceList.setBackgroundColor(Color.CYAN);
    TableLayout deviceTable = new TableLayout(getApplicationContext());
    deviceTable.setId(951357);
    TableRow tr = new TableRow(getApplicationContext());
    LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    tr.setLayoutParams(layout);
    TextView tv = new TextView(getApplicationContext());
    tv.setText("Searching");
    tv.setVisibility(1);
    tr.addView(tv);
    deviceTable.addView(tr);
    deviceList.addView(deviceTable);
    Thread.sleep(1000)
    deviceTable.removeAllViews();
    LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    TableRow tr = new TableRow(getApplicationContext());
    TextView tv = new TextView(getApplicationContext());
    tv.setLayoutParams(layout);
    tr.setLayoutParams(layout);
    tv.setText("No more devices");
    if(rowCounter%2==0)
    {
        System.out.println("rowCounter%2==0");
        tv.setTextColor(Color.WHITE);
        tr.setBackgroundColor(Color.DKGRAY);
    }
    else
    {
        System.out.println("rowCounter%2==1");
        tv.setTextColor(Color.WHITE);
        tr.setBackgroundColor(Color.GRAY);
    }

不工作

    tv.setVisibility(1);
    tv.setEnabled(true);
    tr.addView(tv);
    tr.setVisibility(1);
    deviceTable.addView(tr);

再次工作

    System.out.println(tv+" "+tr+" "+deviceTable+" "+tv.getText());
    deviceTable.setBackgroundColor(Color.BLACK);
    System.out.println("No more devices");

编辑:

tv.setVisibility(View.VISIBLE);
tv.setEnabled(true);
tr.addView(tv);
tr.setVisibility(View.VISIBLE);
deviceTable.addView(tr);
System.out.println(tv+" "+tr+" "+deviceTable+" "+tv.getText());
deviceTable.setBackgroundColor(Color.BLACK);
System.out.println("No more devices");
4

2 回答 2

0

利用

tr.setVisibility(View.VISIBLE);

代替

tr.setVisibility(1);

请注意, View.VISIBLE 的常量值是 0 而不是 1。因此,如果您设置 1,那么您将无法获得正确的结果。所以不要使用 int 总是使用View.VISIBLE, View.INVISIBLE, View.GONE. 它既可以提高可读性,又可以减少出错的机会

于 2013-05-12T18:11:48.903 回答
0

错误:

LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tv.setLayout(layout);

错误是:

The TableRow Layout Params being applied to TextView.
于 2013-05-14T12:56:06.647 回答