2013-08-07 4 views
0

Il existe 3 fichiers xml. Main.xml a une disposition linéaire.
Button.xml contient juste un bouton.
txt.xml contient juste un texte d'édition. Je gonfle button.xml à Main.xml
Ça a l'air bien. Le bouton a les mêmes dimensions que celles du Button.xml.
Ensuite, je gonfle le texte d'édition à Main.xml qui a un ems = 10 (android:ems="10").
Lorsque je cours, le bouton change sa taille pour être identique à celle du texte d'édition.
Comment cela se produit-il et comment surmonter le problème? le code est collé.Mise en page Inflater dans Android

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" 
     tools:context=".First" /> 
    <LinearLayout 
     android:id="@+id/ll" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </LinearLayout> 

</LinearLayout> 

Button.xml

<?xml version="1.0" encoding="utf-8"?> 


    <Button xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     /> 

txt.xml

<?xml version="1.0" encoding="utf-8"?> 

    <EditText xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
    > 

     <requestFocus /> 
    </EditText> 

Main.java

public class First extends Activity { 
    LinearLayout lout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_first); 
     final LayoutInflater inf=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     Button btn=(Button)inf.inflate(R.layout.button, null); 
     lout=(LinearLayout)findViewById(R.id.ll); 

     lout.addView(btn); 
     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(lout.getChildAt(5)==null){ 
        EditText e=(EditText)inf.inflate(R.layout.txt, null); 
        lout.addView(e); 
       } 
      } 
     }); 

    } 

sur 5 clics du bouton, 5 textes d'édition apparaîtront. mais au moment où je clique sur le bouton, la taille du bouton change en plus de la formation de texte d'édition.

+0

Veuillez ajouter votre code ici. –

+0

A collé le code – JayadevKV

Répondre

0

Veuillez coller votre code ici, seulement alors n'importe qui peut vous aider. Encore vous pouvez définir une layout_height & layout_width du bouton à quelque chose comme 20sp (selon votre besoin).

Dans votre fichier Main.xml, vous devez corriger la largeur de la mise en page car la mise en page est petite au début car la vue est petite. Mais lorsque vous cliquez sur le bouton, la mise en page augmente en fonction de la largeur requise pour l'EditText. La taille du bouton augmente également:

<LinearLayout android:id="@+id/ll" 
       android:orientation="vertical" 
       android:layout_width="100sp" 
       android:layout_height="wrap_content" > 
</LinearLayout>