Je tente de créer un groupe de vues composé après avoir gonflé le groupe à partir d'un fichier XML. Le groupe de vues est composé comme suit: Une racine LinearLayout, 2 LinearLayouts enfant. Je suis en mesure de voir la mise en page correctement dans l'éditeur de mise en page; Toutefois, lorsque j'essaie d'ajouter une vue (par exemple un bouton) à partir de l'éditeur, la vue n'apparaît pas et l'application force immédiatement la fermeture. On m'a dit que je devrais peut-être remplacer la méthode onLayout pour dessiner correctement les composants de la vue, mais je suis assez confus.Comment remplir un groupe de vues composé s'étendant de LinearLayout?
Ma Classe:
public class FocusBox extends LinearLayout {
private LinearLayout rootLayout,
contentLayout,
topLayout;
public FocusBox(Context context)
{
super(context, null);
}
public FocusBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.focus_box, this);
rootLayout = (LinearLayout)findViewById(R.id.focusBoxParent);
contentLayout = (LinearLayout)findViewById(R.id.focusBottom);
topLayout = (LinearLayout)findViewById(R.id.focusTop);
}
}
Et le xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:id="@+id/focusBoxParent"
android:orientation="vertical">
<LinearLayout
android:background="@drawable/gradients"
android:layout_weight=".1"
android:gravity="center"
android:id="@+id/focusTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="TextView"
android:id="@+id/focusTitle"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".9"
android:id="@+id/focusBottom"
android:background="@drawable/gradient2"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Avez-vous résolu le problème pour le moment? J'ai juste essayé le même code que vous avez posté ci-dessus et cela fonctionne parfaitement. Je n'ai pas copié votre code, mais je suis arrivé à cette solution indépendamment, et le code résultant est presque parfaitement le même. J'ai vérifié les différences notables, mais il n'y en a pas. – stefs