2012-12-06 1 views
1

J'ai un problème de mise en page avec ma classe de groupes de vues personnalisée.Android: Ajout d'une vue déplacée de la présentation XML à Viewgroup qui n'a pas encore défini de largeur

J'appelle init() dans le constructeur. Dans ma fonction init(), je gonfle la disposition des viewgroups à partir d'un fichier xml, qui contient un LinearLayout auquel j'ajoute plusieurs autres vues.

J'utilise entretoise (Voir layout_width = "0" et layout_weight = "1") pour distribuer les articles equaly.

Le problème réside ici: la disposition n'a toujours pas défini de largeur quand j'ajoute les enfants. Ainsi, les espaceurs ont tous la taille 0 et ne placeront pas les éléments "line_dot" de la même manière. Comment puis-je mettre à jour leur taille?

public class SliderSwitch extends FrameLayout { 

... 
    public SliderSwitch(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    private void init(Context context) 
    { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.slider_switch,this); 


     sliderLayout = (LinearLayout) findViewById(R.id.sliderLayout); 
     labelLayout = (LinearLayout) findViewById(R.id.labelLayout); 


     // add one spacer 
     View spacer = (View) inflate(context,R.layout.spacer, null); 
     sliderLayout.addView(spacer); 

     // setup the view depending on how many elements there are 
     for (int i=1;i<numberOfElements-1;i++) { 

      ImageView lineDot = (ImageView) inflater.inflate(R.layout.slider_switch_line_dot, null); 
      sliderLayout.addView(lineDot); 

      View spacer = (View) inflate(context,R.layout.spacer, null); 
      sliderLayout.addView(spacer); 
     } 

    } 
... 
} 

c'est le fichier xml pour l'entretoise:

<View 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

et c'est celui de ma viewgroup mise en page

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/relLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="3dp" 
      android:orientation="horizontal" > 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/left_bg" /> 

      <LinearLayout 
       android:id="@+id/sliderLayout" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="@drawable/middle_bg" 
       android:gravity="center" 
       android:orientation="horizontal" > 

       <!-- <include layout="@layout/spacer"/> --> 

      </LinearLayout> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/right_bg" /> 
     </LinearLayout> 

     <ImageButton 
      android:id="@+id/sliderKnob" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="0dp" 
      android:background="@null" 
      android:src="@drawable/knob" /> 
    </RelativeLayout> 

</LinearLayout> 

Répondre

0

Vous pouvez appeler init() sur le rappel onSizeChanged. N'oubliez pas de définir et de conserver un drapeau afin d'éviter de l'appeler deux fois, car ce rappel peut s'exécuter plusieurs fois.

+0

bonne idée. Je l'ai essayé. mais je suppose que le problème réside ailleurs parce que je ne suis toujours pas en mesure d'obtenir l'entretoise fonctionne correctement. :( –

+0

c'est étrange, quand j'ajoute les espaceurs directement dans le fichier de disposition en utilisant tout semble bien, mais si je les ajoute dans le code, ils ne fonctionnent pas –

+0

Vous définissez la hauteur de l'entretoise à zéro, Dans ce cas, l'attribut weight n'affecte que la largeur, puisque vous utilisez une orientation horizontale, peu importe que votre vue soit horizontale si height = 0. –

2

J'ai trouvé la solution maintenant. Vous devez redéfinir LayoutParams dans le code (même s'ils sont déjà définis dans le fichier xml), alors tout est soigneusement espacé, comme c'est supposé le faire. semble être un bug. Mais cette solution fonctionne:

// add one spacer 
    View spacer = (View) inflate(context,R.layout.spacer, null); 
    spacer.setLayoutParams(new LinearLayout.LayoutParams(1,LayoutParams.WRAP_CONTENT,1.0F)); 
    sliderLayout.addView(spacer); 

    // setup the view depending on how many elements there are 

    for (int i=1;i<numberOfElements-1;i++) { 

     ImageView lineDot = (ImageView) inflater.inflate(R.layout.slider_switch_line_dot, null); 

     sliderLayout.addView(lineDot); 

     spacer = (View) inflate(context,R.layout.spacer, null); 
     spacer.setLayoutParams(new LinearLayout.LayoutParams(1,LayoutParams.WRAP_CONTENT,1.0F)); 
     sliderLayout.addView(spacer); 
    } 
Questions connexes