web-dev-qa-db-fra.com

L'ajout de LinearLayout par programmation dans Android ne fonctionne pas

La hiérarchie est comme ceci:

  • Disposition relative
    • Disposition linéaire (verticale)
      • FrameLayout (poids 5)
        • ImageView
      • Voir (poids 1)

La vue est juste une vue fictive à des fins d'espacement. Je l'ai fait dans la mise en page xml et cela fonctionne. Mais quand je veux le faire par programme, les codes suivants ne fonctionnent pas.

LinearLayout LL = new LinearLayout(this);
ImageView ladder = new ImageView(this);
FrameLayout ladderFL = new FrameLayout(this);
View dummyView = new View(this);
ladder.setImageResource(R.drawable.ladder1);
LL.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0);
ladderFLParams.weight = 5f;
LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(0,0);
dummyParams.weight = 1f;

FrameLayout.LayoutParams ladderParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
ladder.setLayoutParams(ladderParams);
ladderFL.setLayoutParams(ladderFLParams);
dummyView.setLayoutParams(dummyParams);
LL.setWeightSum(6f);
LL.setLayoutParams(LLParams);

ladderFL.addView(ladder);
LL.addView(ladderFL);
LL.addView(dummyView);
((RelativeLayout) findViewById(R.id.screenRL)).addView(LL);
23
Tommy
LinearLayout LL = new LinearLayout(this);
    LL.setBackgroundColor(Color.CYAN);
    LL.setOrientation(LinearLayout.VERTICAL);

    LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

    LL.setWeightSum(6f);
    LL.setLayoutParams(LLParams);


    ImageView ladder = new ImageView(this);
    ladder.setImageResource(R.drawable.ic_launcher);

    FrameLayout.LayoutParams ladderParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
    ladder.setLayoutParams(ladderParams);

    FrameLayout ladderFL = new FrameLayout(this);
    LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0);
    ladderFLParams.weight = 5f;
    ladderFL.setLayoutParams(ladderFLParams);       
    ladderFL.setBackgroundColor(Color.GREEN);
    View dummyView = new View(this);

    LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0);
    dummyParams.weight = 1f;
    dummyView.setLayoutParams(dummyParams);
    dummyView.setBackgroundColor(Color.RED);



    ladderFL.addView(ladder);
    LL.addView(ladderFL);
    LL.addView(dummyView);
    RelativeLayout rl=((RelativeLayout) findViewById(R.id.screenRL));
    rl.addView(LL);

Je viens d'arranger votre code pour une meilleure compréhension, lui ai également donné une couleur de fond pour obtenir une image claire car je ne sais pas ce que vous voulez, vous pouvez le parcourir. J'espère que c'est utile. Vous devez fournir votre fichier XML de travail afin que nous sachions exactement ce que vous voulez.

61
Piyush