2013-11-20 4 views
0

Я знаю его простой вопрос, но я новичок ..... я хочу добавить кнопку для каждой строки, например, для строки1 должна быть кнопка1, а для строки2 должна быть кнопка2, вставка кнопок и текст управляются с помощью цикла first.xmlКак добавить кнопку для каждого TableRow динамически с помощью Android?

<LinearLayout 
    android:id="@+id/summaryHeaderLinear_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TableLayout 
     android:id="@+id/summaryTable_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="*"> 
    </TableLayout> 

firstrow.xml

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/summary_tablerow" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
    android:id="@+id/summaryHeader_linearlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
    android:id="@+id/summaryHeader_firstlinearlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    </LinearLayout> 
    <LinearLayout 
    android:id="@+id/summaryHeader_secondlinearlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    </LinearLayout> 
    <LinearLayout 
    android:id="@+id/summaryHeader_thirdlinearlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    </LinearLayout> 
    </LinearLayout> 
</TableRow> 

firstRow.java:

 setContentView(R.layout.first); 
    summaryTableLayout = (TableLayout)findViewById(R.id.summaryTable_layout); 
    summaryInflator = getLayoutInflater(); 
    for(int i =0;i<2;i++) 
    { final TableRow summaryTableRow = (TableRow)summaryInflator.inflate(R.layout.firstrow,null); 
LinearLayout summaryHeaderLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_linearlayout); 
summaryTableRow.setId(i); 
Button summaryBtn = new Button(this); 
summaryBtn.setId(i); 
summaryBtn.setText("sample button:" +i); 
summaryBtn.setBackgroundColor(Color.MAGENTA); 
summaryTableRow.addView(summaryBtn); 
for(int k=0;k<5;k++) 
{ 
LinearLayout summaryFirstHorizontalLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_firstlinearlayout); 
TextView newText = new TextView(this); 
newText.setId(k); 
newText.setText(topRowArray[k]); 
newText.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,1f)); 
newText.setBackgroundColor(Color.BLACK); 
newText.setTextColor(Color.WHITE); 
summaryFirstHorizontalLinearLayout.addView(newText); 
} 
summaryTableLayout.addView(summaryTableRow); 
} 

выход код: code snaphot
в то время как я хочу, чтобы создать этот макет:

Button for row1 
     name qty cost sell margin 
    Button for row2 
    name qty cost sell margin 

, но его не показывая, как это я уже определили отдельные XML я ДНТ хочу создайте динамический Row.I хотите использовать определенный firstrow.xml, пожалуйста, помогите мне, что мне нужно делать изменения в моем существующем коде

+0

Вы можете добавить их с помощью кода динамически. См. Ответ @ ravi-patel ниже. Но для такого макета андроиды создали «ListView». Это будет вариант? – jboi

ответ

0

использовать для динамического добавления textv МЭН и кнопка на LinearLayout

LinearLayout ll = (LinearLayout)findViewById(R.id.layout); 
    for(int y =0;y<5;y++){ 
     TextView textv= new TextView(this); 
     final Button myButton = new Button(this); 

     textv.setText("record"); 
     myButton.setText("button"+y); 
      LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     ll.addView(textv,lp); 
     ll.addView(myButton,lp); 
} 

я думаю, что это имеет смысл использовать ваше решение проблемы

+0

может у меня помочь мне, как я могу создать с помощью TableRow – nida

+0

привет господин я не ваш сотрудник, я могу только переписать этот процесс, чтобы динамически добавить кнопку, используя этот код, чтобы создать новый собственный код требования ok –

Смежные вопросы