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.
Veuillez ajouter votre code ici. –
A collé le code – JayadevKV