2012-09-11 50 views
0

對不起,我的英語不好。我有一個表格行,我用記錄的代碼設置了第一個視圖。我用0px高度和0px寬度設置了這個視圖。但它需要我一半的屏幕!爲什麼?錯誤在哪裏?用0height和0width查看並不隱藏

這是代碼:

 try{ 

     txtId= new TextView(this); 
     imgCategory= new ImageView(this); 
     txtDescrizione= new TextView(this); 
     txtDataFine= new TextView(this); 
     imgPriorita= new ImageView(this); 

     txtId.setText(id); 
     txtId.setWidth(0); 
     txtId.setHeight(0); 
     txtId.setTextColor(getResources().getColor(R.color.white)); 
     txtId.setTextSize(1); 


     if(priorita.equalsIgnoreCase("3")){ 
      imgPriorita.setImageResource(R.drawable.todo_priority_3);} 
     else if(priorita.equalsIgnoreCase("2")){ 
      imgPriorita.setImageResource(R.drawable.todo_priority_2);} 
     else if(priorita.equalsIgnoreCase("1")){ 
      imgPriorita.setImageResource(R.drawable.todo_priority_1);} 

     imgPriorita.setPadding(0, 0, 0, 0); 
     imgPriorita.setMaxWidth(widthDisplay/10); 
     imgPriorita.setMaxHeight(40); 
     imgPriorita.setScaleType(ScaleType.CENTER_INSIDE); 


     txtDescrizione.setPadding(0, 0, 0, 0); 
     //txtDescrizione.setText(Html.fromHtml("<i>"+descrizione+"</i>")); 
     txtDescrizione.setText(descrizione); 
     txtDescrizione.setTextColor(getResources().getColor(R.color.black)); 
     txtDescrizione.setWidth(widthDisplay/5); 
     txtDescrizione.setHeight(40); 
     txtDescrizione.setTextSize(12); 
     //setto la descrizione su una sola riga e con true fino a quando ci stanno le lettere 
     txtDescrizione.setSingleLine(true); 
     txtDescrizione.setLines(1); 


     txtDataFine.setPadding(0, 0, 0, 0); 
     txtDataFine.setText(data.getDateTimeTodo(dataFine)); 
     txtDataFine.setWidth(widthDisplay/5); 
     txtDataFine.setHeight(40); 
     txtDataFine.setTextColor(getResources().getColor(R.color.black)); 
     txtDataFine.setGravity(Gravity.CENTER); 
     txtDataFine.setTextSize(12); 

     if(category.equalsIgnoreCase("2")){ 
      imgCategory.setImageResource(R.drawable.todo_category_2);} 
     else if(category.equalsIgnoreCase("1")){ 
      imgCategory.setImageResource(R.drawable.todo_category_1);} 
     else if(category.equalsIgnoreCase("3")){ 
      imgCategory.setImageResource(R.drawable.todo_category_3);} 
     else if(category.equalsIgnoreCase("5")){ 
      imgCategory.setImageResource(R.drawable.todo_category_5);} 
     else if(category.equalsIgnoreCase("4")){ 
      imgCategory.setImageResource(R.drawable.todo_category_4);} 
     else if(category.equalsIgnoreCase("6")){ 
      imgCategory.setImageResource(R.drawable.todo_category_6);} 



     imgCategory.setPadding(0,0,0,0); 
     imgCategory.setMaxWidth(widthDisplay/5); 
     imgCategory.setMaxHeight(40); 
     imgCategory.setScaleType(ScaleType.CENTER_INSIDE); 

     rowTodo[counterBackground]= new TableRow(this); 
     if((counterBackground%2)==0){ 
      rowTodo[counterBackground].setBackgroundColor(getResources().getColor(R.color.greyListCp));} 
     else{ 
      rowTodo[counterBackground].setBackgroundColor(getResources().getColor(R.color.white));} 

     rowTodo[counterBackground].setMinimumHeight(heightRowTodo); 
     idRow_gl=Integer.parseInt(id); 
     rowTodo[counterBackground].setId(idRow_gl); 

     rowTodo[counterBackground].setOnClickListener(new OnClickListener() { 
      public void onClick(View view_cliccata) { 
        onclickRow(view_cliccata); 
      } 
     }); 


     imgAnagraficaTodo[counterBackground]=new ImageView(this); 

     if(id_anagTodo.equals("")){} 
     else{ 
      imgAnagraficaTodo[counterBackground].setImageResource(R.drawable.toanag); 
      imgAnagraficaTodo[counterBackground].setPadding(20, 3, 0, 0); 
      imgAnagraficaTodo[counterBackground].setMinimumWidth(widthDisplay/5); 
      imgAnagraficaTodo[counterBackground].setMaxHeight(40); 
      imgAnagraficaTodo[counterBackground].setId(idRow_gl); 
      imgAnagraficaTodo[counterBackground].setScaleType(ScaleType.CENTER_INSIDE); 

      imgAnagraficaTodo[counterBackground].setOnClickListener(new OnClickListener() { 
       public void onClick(View view_cliccata) { 
         Data.myHashMapVar.put("ID_TODO", view_cliccata.getId()); 
         onclickImgAnagraficaTodo(view_cliccata,id_anagTodo); 
       } 
      }); 
      arrList_AnagTodo.add(id_anagTodo); 
     } 

     rowTodo[counterBackground].addView(txtId); 
     rowTodo[counterBackground].addView(imgPriorita); 
     rowTodo[counterBackground].addView(txtDescrizione); 
     rowTodo[counterBackground].addView(txtDataFine); 
     rowTodo[counterBackground].addView(imgCategory); 
     rowTodo[counterBackground].addView(imgAnagraficaTodo[counterBackground]); 


     List_TodoList.addView(rowTodo[counterBackground]); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

而且heightDisplay和widthDisplay以這種方式設置:提前

display=getWindowManager().getDefaultDisplay(); 
      heightDisplay=display.getHeight(); 
      widthDisplay=display.getWidth(); 

謝謝!

回答

1

如果你不想走了。爲什麼不只是使用

txtId.setVisibility(View.GONE); 

+0

感謝它的工作! :) –

+0

但是,@marmor寫道,你應該真的在xml中聲明你的佈局。只有在通用創建東西時,您可能會被迫在代碼中執行此操作。 –

+0

我已經宣佈它@ _ @。我已經在我的xml中聲明瞭tablelayout,我只是通過代碼添加行... :) –