[英]Adding multiple views to a LinearLayout android
I have a problem adding my TableLayout to a parent LinearLayout. 我将TableLayout添加到父级LinearLayout时遇到问题。 Only the first view is added to the LinearLayout. 仅第一个视图被添加到LinearLayout。 I want to create as many TableLayouts as the response.length() is and then add them to the parent LinearLayout. 我想创建与response.length()一样多的TableLayouts,然后将它们添加到父LinearLayout中。
Here's the Java code: 这是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 codes for the layouts: 布局的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: 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>
Why you don't use TableRow
inside TableLayout
instead of multiple TableLayout
? 为什么不在TableLayout
使用TableRow
而不是多个TableLayout
?
Refer to How to add rows dynamically into table layout 请参阅如何将行动态添加到表布局中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.