2012-10-22 62 views
0

我有表格佈局,它有幾行,每行包含一個textView和一個editText。我有一個空行,我想用textView和editText動態填充。我添加了一個textView(臀部),但沒有顯示其他人。見下面,在第三行textView顯示爲trasnparent。我想要動態地添加與上述行相同的textView和editText。dynamiclly在表格行中添加視圖

enter image description here

TableRow row = (TableRow) findViewById(R.id.tblHips); 
    TextView txt=new TextView(getApplicationContext()); 
    txt.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceLarge); 
    txt.setId(R.id.txtHipId); 
    txt.setText("Hips"); 
    row.addView(txt); 

我加入的代碼從腰部僅排downwards-

 <TextView 
      android:id="@+id/TextView02" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:text="Waist" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <EditText 
      android:id="@+id/EditText02" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:inputType="numberDecimal" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tblHips" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioGroup 
      android:id="@+id/rdMs" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+id/radio0" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:checked="true" 
       android:text="Imperial" /> 

      <RadioButton 
       android:id="@+id/radio1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Metric" /> 
     </RadioGroup> 

    </TableRow> 
+0

我們可以看到您的佈局文件或者至少包含圖像中某行的部分嗎? – Luksprog

+0

請參閱編輯後的文章 – Tanvir

+0

在這個'LayoutParams'中添加'TextView'有什麼區別? 'row.addView(txt,new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));' – Luksprog

回答

0

之所以TextView看起來透明是因爲文本的阿爾法未設置爲255. txt.setAlpha(255);將解決這個問題。

如果你只是有一個隱藏的領域,解決這個問題的最好方法是把所有的視圖放在xml中,並用android:visibility="gone"'隱藏'隱藏的視圖。然後在你的代碼中,你可以在View.GONEView.VISIBLE之間切換(你想使用不見了,因爲它從佈局中刪除了視圖)。

如果您確實需要在運行時添加視圖,您需要指定每個小小的細節,如文本大小(並用不同的dpi進行更改),文本顏色/ alpha等等,這很快就會變成一個噩夢來支持。在這種情況下,您應該製作一個單獨的佈局.xml文件並使用佈局充填器來生成視圖。

LayoutInflater inflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
TableRow row = (TableRow)inflater.inflate(R.layout.hips, container, false); 
//where 'container' is the containing TableView & 'this' is an activity 
+0

我使用xml佈局的行內容,並添加這個像 - View v = inflater.inflate(R.layout.layouthips,null); row.addView(v);這很好。但View.GONE View.VISIBLE方法看起來不錯。 – Tanvir

+0

太棒了!如果你沒有做任何動態的(由用戶生成多個新行);它可能會更容易定義一個XML中的所有行。然後你可以隱藏/顯示可視性。 – Ancantus

相關問題