Adding LinearLayout programmatically in Android doesn't work

后端 未结 1 1213
独厮守ぢ
独厮守ぢ 2020-12-01 07:48

The hierarchy is like this:

  • RelativeLayout
    • LinearLayout (vertical)
      • FrameLayout (weight 5)
        • ImageView
      • View (weight
相关标签:
1条回答
  • 2020-12-01 08:45
    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);
    

    I have just arranged your code for better understanding, also gave it a background color to get the clear picture as I dont know what you want, you can go through it. I Hope it is helpful. You should provide your working xml so we know exactly what you want.

    0 讨论(0)
提交回复
热议问题