vistas extender android dynamic view android-layout

extender - Android: agregue dinĂ¡micamente vistas a la vista



extender vistas en android (5)

Tengo un diseño para una vista -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0px" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/items_header" style="@style/Home.ListHeader" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/items_none" android:visibility="gone" style="@style/TextBlock" android:paddingLeft="6px" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/items_list" /> </LinearLayout>

Lo que quiero hacer es en mi actividad principal con un diseño como este

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0px" android:id="@+id/item_wrapper"> </LinearLayout>

Quiero recorrer mi modelo de datos e inyectar múltiples vistas que consisten en el primer diseño en el diseño principal. Sé que puedo hacer esto construyendo los controles completamente dentro del código, pero me preguntaba si había una manera de construir dinámicamente las vistas para poder continuar usando un diseño en lugar de poner todo en código.


Para que la respuesta de @Mark Fisher sea más clara, la vista insertada que se está inflando debe ser un archivo xml en la carpeta de diseño pero sin un diseño (ViewGroup) como LinearLayout, etc. en el interior. Mi ejemplo:

res / layout / my_view.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/i_am_id" android:text="my name" android:textSize="17sp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/>

Entonces, el punto de inserción debe ser un diseño como LinearLayout:

res / layout / activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/aaa" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/insert_point" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> </RelativeLayout>

Entonces el código debería ser

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping_cart); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.my_view, null); ViewGroup main = (ViewGroup) findViewById(R.id.insert_point); main.addView(view, 0); }

La razón por la que publico esta respuesta muy similar es que cuando traté de implementar la solución de Mark, me quedé atrapado en el archivo xml que debería usar para insert_point y la vista secundaria. Usé el diseño en la vista secundaria en primer lugar y no funcionaba en absoluto, lo que me llevó varias horas averiguarlo. Así que espero que mi exploración pueda salvar el tiempo de otros.



Use el LayoutInflater para crear una vista basada en su plantilla de diseño y luego inyéctela en la vista donde la necesite.

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.your_layout, null); // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.a_text_view); textView.setText("your text"); // insert into main view ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point); insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

Es posible que deba ajustar el índice donde desea insertar la vista.

Además, establezca los LayoutParams de acuerdo con la forma en que le gustaría que se ajuste en la vista principal. por ejemplo, con FILL_PARENT , MATCH_PARENT , etc.


Ver la clase LayoutInflater .

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert); inflater.inflate(R.layout.the_child_view, parent);


// Parent layout LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout); // Layout inflater LayoutInflater layoutInflater = getLayoutInflater(); View view; for (int i = 1; i < 101; i++){ // Add the text layout to the parent layout view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false); // In order to get the view we have to use the new view with text_layout in it TextView textView = (TextView)view.findViewById(R.id.text); textView.setText("Row " + i); // Add the text view to the parent layout parentLayout.addView(textView);