У меня есть xml-файл LinearLayout, который включает в себя TableLayout, а позднее - TableRows. Поэтому я хочу, чтобы новый TableRow был добавлен программно при запуске приложения.TableLayout addView не работает
Таким образом, я написал следующий код, используя метод addView, но мой код не работает.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout myRoot = (LinearLayout)findViewById(R.id.tbl_test);
TableRow a = new TableRow(this);
a.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myRoot.addView(a);
}
}
XML-файл
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<TableLayout
android:id="@+id/tbl_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tbr_test"
android:background="#ffffff" android:layout_margin="1dp">
<TextView android:layout_column="0" />
<TextView android:layout_column="1" />
<TextView android:layout_column="2" />
<TextView android:layout_column="3" />
</TableRow>
<TableRow
android:background="#ffffff"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp">
<TextView android:layout_column="0" />
<TextView android:layout_column="1" />
<Button/>
<Button/>
</TableRow>
<TableRow
android:background="#ffffff"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:paddingRight="2dp">
<TextView android:layout_column="0" />
<TextView android:layout_column="1" />
<Button/>
<Button />
</TableRow>
<TableRow
android:background="#ffffff"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:paddingRight="3dp">
<TextView android:layout_column="0" />
<TextView android:layout_column="1" />
<Button />
<Button />
</TableRow>
<TableRow
android:id="@+id/tbr"
android:background="#ffffff"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:paddingRight="4dp">
<TextView android:layout_column="0" />
<TextView android:layout_column="1" />
<Button />
<Button/>
</TableRow>
</TableLayout>
</LinearLayout>
Нет, он не работает для меня – JavaFan
@JavaFan Какая у вас проблема после добавления этого вопроса. – Ironman
Нет проблем, но не добавлена новая строка, потому что минимальное значение api равно 8? но я добавляю эту строку @TargetApi (Build.VERSION_CODES.HONEYCOMB) – JavaFan