2011-10-13 9 views
5

tengo diseño y quiero inflar queInflater inflar pero la altura es pequeña (se parece a wrap_content y necesitan fill_parent)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    >  
    </EditText> 

</LinearLayout> 

como

LinearLayout ll=(LinearLayout)findViewById(R.id.llContainer); 
    View view; 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.question_free_text, null); 
    ll.addView(view); 

donde ll es

<LinearLayout 
    android:id="@+id/llContainer" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="20dp" 
    android:layout_marginBottom="20dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
> 
</LinearLayout> 

en otro xml, pero el problema es cuando se infla, pero la altura es grande (fill_parent, parece wrap_content, pero no hay wrap_content en diseño). Alguien puede ayudarme ?

+0

podría ser debido a estos 2tags .. androide: layout_marginTop = "20dp" androide: layout_marginBottom = "20dp" – ngesh

+2

reemplaza null con vista principal, durante el inflado. –

Respuesta

14

Como Yashwanth Kumar mencionó correctamente en los comentarios, el segundo parámetro de la infle-método debe ser el punto de vista de la raíz en el que se inserta la nueva vista:

LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer); 
View view; 
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.question_free_text, ll); 

Si se proporciona una vista de la raíz en el inflate-call, el LayoutInflator llama al método generateLayoutParams(ViewGroup.LayoutParams p) de esta vista raíz para obtener algunos LayoutParams (que contienen básicamente información sobre cuán grande puede/debe ser) una vista que se pasará a la nueva vista.

Tenga en cuenta que si proporciona una vista de raíz, la vista inflada se agregará automáticamente a la vista raíz a través del root.addView(View child, LayoutParams params).

También se puede pasar un tercer parámetro al método de inflado-(boolean attachToRoot), si este valor es false, el nuevo punto de vista no se añadirá automáticamente a la vista de la raíz, pero las LayoutParams todavía se fijan con setLayoutParams(params). Puede usar esta opción si varita para añadir manualmente ve a la vista raíz (por ejemplo, en una posición/índice específico):

LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer); 
View view; 
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.question_free_text, ll, false); // the LayoutParams of view are set here 
ll.addView(view, 2); 
+1

excelente respuesta, respuesta correcta. – VinceStyling

Cuestiones relacionadas