Я знаю его простой вопрос, но я новичок ..... я хочу добавить кнопку для каждой строки, например, для строки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);
}
выход код:
в то время как я хочу, чтобы создать этот макет:
Button for row1
name qty cost sell margin
Button for row2
name qty cost sell margin
, но его не показывая, как это я уже определили отдельные XML я ДНТ хочу создайте динамический Row.I хотите использовать определенный firstrow.xml, пожалуйста, помогите мне, что мне нужно делать изменения в моем существующем коде
Вы можете добавить их с помощью кода динамически. См. Ответ @ ravi-patel ниже. Но для такого макета андроиды создали «ListView». Это будет вариант? – jboi