how to set setLayoutParams for linear layout elements

后端 未结 2 488
悲哀的现实
悲哀的现实 2020-12-09 16:04

How to set setLayoutParams() for LinearLayout elements. in MainActivity.java. I wrote the following code for set Layout params and

相关标签:
2条回答
  • 2020-12-09 16:27
    imageview1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));  
    textview1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));
    listview01.setLayoutParams(new
           LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    
    0 讨论(0)
  • 2020-12-09 16:27

    Try this in onCreate at the beginning of your activity (should replace the pic though). Don't forget to comment the setContentView that comes by default:

    // setContentView(R.layout.activity_main);
    
    TextView label = new TextView(this);
    label.setText("This is working, congrats!");
    label.setTextSize(20);
    label.setGravity(Gravity.CENTER_HORIZONTAL);
    
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(new WindowManager.LayoutParams());
    
    ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    ll.setGravity(Gravity.CENTER);
    ll.addView(label);
    setContentView(ll);
    
    0 讨论(0)
提交回复
热议问题