How to add a button dynamically in Android?

后端 未结 17 1357
礼貌的吻别
礼貌的吻别 2020-11-22 13:17

How to add a button dynamically in Android?

17条回答
  •  广开言路
    2020-11-22 13:55

    Check this up.

    LinearLayout ll_Main  = new LinearLayout(getActivity());
    LinearLayout ll_Row01 = new LinearLayout(getActivity());
    LinearLayout ll_Row02 = new LinearLayout(getActivity());
    
    ll_Main.setOrientation(LinearLayout.VERTICAL);
    ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
    ll_Row02.setOrientation(LinearLayout.HORIZONTAL);
    
    final Button button01    = new Button(getActivity());
    final Button button02    = new Button(getActivity());   
    final Button button03    = new Button(getActivity());
    final Button button04    = new Button(getActivity());
    
    ll_Row01.addView(button01);
    ll_Row01.addView(button02);
    
    ll_Row02.addView(button03);
    ll_Row02.addView(button04);
    
    ll_Main.addView(ll_Row01);
    ll_Main.addView(ll_Row02);
    
    button04.setVisibility(View.INVISIBLE);
    button04.setVisibility(View.VISIBLE);
    

提交回复
热议问题