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

エラー:

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 に答える
0

使用する

tr.setVisibility(View.VISIBLE);

それ以外の

tr.setVisibility(1);

View.VISIBLE の定数値は 1 ではなく 0 であることに注意してください。したがって、1 を設定すると、適切な結果が得られません。そのため、 int を使用する代わりに、常にView.VISIBLE, View.INVISIBLE,を使用しView.GONEます。可読性が向上し、エラーの可能性が減少します

于 2013-05-12T18:11:48.903 に答える