how to set setLayoutParams for linear layout elements

后端 未结 2 498
悲哀的现实
悲哀的现实 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

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

提交回复
热议问题