2011-08-19 9 views
5

Estoy tratando de inflar el diseño xml en ViewGroup personalizado. Después de inflar el diseño, ViewGroup solo muestra la vista raíz del diseño; de hecho, debe mostrar el archivo de diseño completo en ViewGroup.Inflar el diseño XML en un grupo de vista personalizado

He pasado por otras preguntas similares publicadas relacionadas con esto en stackoverflow y otros sitios web, pero ninguno me ayudó.

Aquí está el código de mi costumbre ViewGroup:

public class ViewEvent extends ViewGroup { 
private final String LOG_TAG="Month View Event"; 
public ViewEvent(Context context) { 
     super(context); 
    } 

    public ViewEvent(Context context,AttributeSet attrs) { 
     super(context,attrs); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) {  

     int childCount= getChildCount(); 
     for(int i=0;i<childCount;i++) 
     { 
      Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b); 
      View v=getChildAt(i); 
      if(v instanceof LinearLayout) 
      { 
       Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1 
       v.layout(0, 0, r-l, b-t); 
      } 
      //v.layout(l, r, t, b); 
     } 
    } 

En una Activity, estoy usando esta vista personalizada grupo:

ViewEvent event = new ViewEvent(getApplicationContext()); 
LayoutInflater inflator; 
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
inflator.inflate(R.layout.try1, event); 
setContentView(event); 

siguiente es el archivo de diseño:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffaa77"> 


    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#ffff0000" 
     android:text="asdasadfas" /> 

</LinearLayout> 

Después de inflar este diseño, solo obtengo la raíz LinearLayout con color de fondo #ffff0000. TextView no se muestra.

¿Dónde estoy mal o me falta algo?

Respuesta

-1

Pruebe de esta manera. No declaró ninguna identificación para su TextView.

<TextView 
      android:id="@+id/testTextId" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#ffff0000" 
      android:text="asdasadfas" /> 

A continuación, acceda a este TextView de la siguiente manera.

TextView testTextView = (TextView) ViewEvent.findViewById(R.id.testTextId); 
+0

Es accesible pero no se muestra! –

2

tratar getParent() del evento (su ViewEvent):

inflator.inflate(R.layout.try1, event.getParent(), false); 
Cuestiones relacionadas