/ / Conversion de fichiers de présentation en Java sous Android - Java, Android, Android-layout

Conversion de fichiers de disposition en Java dans Android - Java, Android, android-layout

Je rencontre un problème pour convertir les fichiers .xml de présentation en Java J'ai essayé mais j'ai échoué. J'ai besoin de l'aide de quelqu'un pour le compléter. Besoin d'aide pour! Merci d'avance.

Voici mon fichier .xml de mise en page:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lay"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox android:id="@+id/checkbox_cheese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheese"
android:onClick="onCheckboxClicked"/>
</LinearLayout>

Je veux juste le convertir en Java que j'ai essayé jusqu'à présent:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lay);
CheckBox box = new CheckBox(this);
box.setId(c);
/* from here how to convert those below lines in Java
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheese"
android:onClick="onCheckboxClicked"
*/

Réponses:

1 pour la réponse № 1

Je suis de bonne humeur ou je vous aurais signalé le commentaire d’Egor; les tutoriels sont conçus pour vous montrer les moyens efficaces de faire ce que vous voulez.

LinearLayout.LayoutParams boxParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
box.setLayoutParams(boxParams);
box.setText(R.string.cheese);

// if you want checkbox change listener
box.setOnCheckedChangedListener(new OnCheckedChangedListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (isChecked){
// perform logic
}
}
};

// if you want on click listener functionality
box.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
// perform logic
}
}

linearLayout.addView(box);

Haven "t testé, ne sait pas si il est sans bug(peut avoir manqué quelque chose ou fait quelque chose de mal), mais je peux vous dire que, sauf si vous envisagez de ne faire que cette fonctionnalité et aucune autre fonctionnalité que vous devez VRAIMENT lire dans les bases d'Android.


0 pour la réponse № 2
    android:layout_width="wrap_content"
android:layout_height="wrap_content"

-> setLayoutParams (..)

    android:text="@string/cheese"

-> setText (..)

    android:onClick="onCheckboxClicked"

-> setOnClickListener (..)


0 pour la réponse № 3

Faites quelque chose comme ça:

View layout = ViewInflater.from(this).inflate(R.layout.layout, null); //your layout name

LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.lay);
CheckBox box = (CheckBox) layout.findViewById(R.id.checkbox_cheese);

Maintenant le box L'objet aura toutes les propriétés assignées dans le XML.