2010-08-06 23 views
23

He leído el truco de la interfaz de usuario de Android 2 para desarrolladores de Android, que le dice a las personas cómo incluir un diseño en otro archivo de diseño varias veces, y le da a estos diseños incluidos una identificación diferente. Sin embargo, la muestra aquí está sobrescribiendo el ID de disposición, no el ID de las vistas EN este diseño. Por ejemplo, si el workspace_screen.xml se ve así:¿Cómo accedo a las vistas dentro del diseño cuando lo vuelvo a utilizar varias veces?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
<TextView android:id="@+id/firstText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="first"/> 
<TextView android:id="@+id/secondText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="second"/> 

Y lo incluyo tres veces en otro archivo de diseño. ¿Termino con tres TextViews con id firstText y otras tres con secondText? ¿No hay una colisión de id? ¿Y cómo puedo encontrar el segundo TextView en el tercer diseño incluido con findViewById? ¿Qué debo ingresar en el método findViewById?

Respuesta

44

Digamos que quiere incluir este:

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
> 
    <ImageView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:src="@drawable/some_image" 
    /> 
    <TextView 
     android:id="@+id/included_text_view" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
    /> 
</LinearLayout> 

por lo que en el código se inserta así:

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
> 
    <include android:id="@+id/header_1" layout="@layout/name_of_layout_xml" /> 
    <include android:id="@+id/header_2" layout="@layout/name_of_layout_xml" /> 
</LinearLayout> 

ahora desea tener acceso a las vistas de texto dentro de los diseños incluidos para fijar el texto dinámicamente En su código sólo hay que escribir:

LinearLayout ll = (LinearLayout)findViewById(R.id.header_1); 
TextView tv = (TextView)ll.findViewById(R.id.included_text_view); 
tv.setText("Header Text 1"); 

ll = (LinearLayout)findViewById(R.id.header_2); 
tv = (TextView)ll.findViewById(R.id.included_text_view); 
tv.setText("Header Text 2"); 

notificación que utilice métodos findViewById los LinearLayouts individuales para limitar la búsqueda a sólo sus hijos.

1

Quería crear el diseño TableRow y quería reutilizarlo en mi código for for loop. Estaba buscando el problema y finalmente encontré la solución.

 TableLayout table = (TableLayout) findViewById(R.id.listTable); 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ImageButton[] button = new ImageButton[4]; 
     TableRow[] row = new TableRow[4]; 

     for(int i =0 ; i<4;i++){ 

       row[i] = (TableRow) inflater.inflate(R.layout.list_row, null); 
       table.addView(row[i]); 
       button[i] = (ImageButton)row[i].findViewById(R.id.editBtn); 
       button[i].setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Log.i(TAG,"Button Clicked"); 
        } 
       }); 

esta manera se puede conseguir la funcionalidad de reutilizar el diseño y también se puede acceder a los elementos interiores

Cuestiones relacionadas