У меня есть два макета, я раздуваю один макет в другой и хочу отображать этот раздутый макет, включая текст, столько раз, сколько условие истинно. Я установил значение textview как значение i, а также установил верхний маркер макета. но он отображает значение i только в одной строке и остаток пуст, а также его setMargin() также не работает. Это то, что я сделал еще.Как отображать макет динамически с полем
public void displayRows(){
int n= 6;
for(int i=0;i< n;i++){
LinearLayout container = (LinearLayout)findViewById(R.id.layoutContainer);
View hiddenInfo = getLayoutInflater().inflate(R.layout.dynamic, container, false);
container.addView(hiddenInfo);
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText("Text"+i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 30, 0, 0);
hiddenLayout.setLayoutParams(layoutParams);
hiddenLayout.addView(textView);
}
container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/layoutContainer"
android:animateLayoutChanges="true"
android:layout_gravity="center|top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/li1"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@color/colorAccent"
android:onClick="displayRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="@color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center"
android:text="Rows"/>
</LinearLayout>
</LinearLayout>
dynamic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/dynamic"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:text=""
android:textColor="@color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Я также хотел бы знать, как удалить все сгенерированные динамические строки на кнопку нажмите кнопку Удалить.
его дает мне IllegalStateException, что указанный ребенок уже имеет родителя. –
Просмотреть мои обновления. –
спасибо, что он работает. также дайте мне знать, как удалить все эти сгенерированные строки при удалении кнопки. –