2013-09-28 2 views
22

funktioniert der Hierarchie ist wie folgt:Linearlayout programmatisch in Android Hinzufügen von nicht

  • RelativeLayout
    • Linearlayout (vertikal)
      • FrameLayout (Gewicht 5)
        • Image
      • Ansicht (Gewicht 1)

The View ist nur ein Dummy-Ansicht für einen bestimmten Zweck Abstand. Ich habe es im Layout-XML und es funktioniert. Aber wenn ich es programmgesteuert machen möchte, funktionieren die folgenden Codes nicht.

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); 

Antwort

58
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); 

Ich habe den Code für ein besseres Verständnis gerade angeordnet sind, gab es auch eine Hintergrundfarbe das klare Bild zu bekommen, wie ich weiß nicht, was Sie wollen, können Sie durch sie gehen. Ich hoffe es ist hilfreich. Sie sollten Ihre Arbeits-XML bereitstellen, damit wir genau wissen, was Sie wollen.

+0

Ich weiß nicht, warum Ihre Codes funktionieren, selbst wenn ich diese Farben gelöscht habe. Vielleicht gibt es ein Problem mit meiner Anordnung von Codes. Wie auch immer, danke! – Tommy

+1

@Piyush Gupta: Was bedeutet screenRL? –