• Android中利用LinearLayout动态添加控件


    在androidUI布局中,一般都是利用xml来布局控件,这是比较方便和直观的,但是有时却需要动态生成,下面就举2个简单例子来说明怎么动态添加控件:
    
     
    
    1.动态添加2个垂直排列的Button
    
    [c-sharp] view plaincopy
     @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
           //setContentView(R.layout.main);        
           final LinearLayout layout2=new LinearLayout(this);  
            layout2.setOrientation(LinearLayout.VERTICAL);  
            Button btn1=new Button(this);  
            setContentView(layout2);  
            Button btn2=new Button(this);  
            btn1.setText("Button1");  
            btn2.setText("Button2");  
            layout2.addView(btn1);  
            layout2.addView(btn2);  
             
              
           setContentView(layout2);  
    }  
    final LinearLayout layout2=new LinearLayou(this);
    
    定义一个LinearLayout ,参数为context在这儿即为this
    
    layout2.setOrientation(LinearLayout.VERTICAL);
    
    设置layout格式为vertical,竖直排列
    
     
    
    2.在Button的click事件中动态添加一个button
    
    [java:showcolumns] view plaincopy
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    OnClickListener listen1;  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
          //setContentView(R.layout.main);        
          final LinearLayout layout2=new LinearLayout(this);  
           layout2.setOrientation(LinearLayout.VERTICAL);  
           Button btn1=new Button(this);  
           setContentView(layout2);  
           Button btn2=new Button(this);  
           btn1.setText("Button1");  
           btn2.setText("Button2");  
           layout2.addView(btn1);  
           layout2.addView(btn2);         
           listen1 = new OnClickListener() {  
            public void onClick(View v) {  
                setTitle("点击button1 ");  
                Button btn3=new Button(v.getContext());  
                layout2.addView(btn3);  
                btn3.setText("Button3");  
                  
                  
            }  
        };  
         btn1.setOnClickListener(listen1);  
       }  
    
    与上一例子区别就是,在Button的OnClickListener中动态添加控件,需要注意的一点是 Button btn3=new Button(v.getContext);参数v.getContext为当前view的context,为什么例子1中用this作为context呢,呵呵,这是因为this即当前的activity,而activity又是context的子类,所以this就可以直接作为activity了。
    
    查了下,context派生的类有:
    
    java.lang.Object
       ↳    android.content.Context
     Known Direct Subclasses
    ContextWrapper , MockContext
     Known Indirect Subclasses
    
    AbstractInputMethodService , Activity , ActivityGroup , AliasActivity , Application , ContextThemeWrapper , ExpandableListActivity , InputMethodService , IntentService , IsolatedContext , LauncherActivity , ListActivity , MockApplication , MutableContextWrapper , PreferenceActivity , RenamingDelegatingContext , Service , TabActivity
  • 相关阅读:
    Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和
    Codeforces Round #358 (Div. 2) A B C 水 水 dfs序+dp
    51nod 1225 数学
    HDU 4584 splay
    bzoj 1588 平衡树 splay
    HDU 4722 数位dp
    Kubernetes 自动伸缩 auto-scaling
    转发一篇能看懂的关于ingress的说明
    Http 安全检测
    mdadm 软RAID
  • 原文地址:https://www.cnblogs.com/nanhai/p/2742172.html
Copyright © 2020-2023  润新知