将多个视图添加到LinearLayout android

时间:2017-06-11 09:44:27

标签: android android-layout

将TableLayout添加到父LinearLayout时遇到问题。 只有第一个视图添加到LinearLayout。 我想创建与response.length()一样多的TableLayouts,然后将它们添加到父LinearLayout。

这是Java代码:

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.pendingReservations);
for (int i = 0; i < response.length(); ++i) {       
    final TableLayout tableLayout = (TableLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.confirm_reservation_view,null);
    //set the TableLayout content
    linearLayout.addView(tableLayout,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

布局的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollViewConfirmRes"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.konrad.rezerwacje1.ConfirmReservation">

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/pendingReservations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>

</ScrollView>

TableLayout:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/confirmReservationTableLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:stretchColumns="1">


    <!-- TableLayout content -->

</TableLayout>

1 个答案:

答案 0 :(得分:0)

为什么你不在TableRow内使用TableLayout而不是多个TableLayout

请参阅How to add rows dynamically into table layout