zoukankan      html  css  js  c++  java
  • 6月9日学习日志

    今天学习了Java代码动态添加控件或xml布局。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <Button
            android:id="@+id/btnLoad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="动态加载布局"/>
    </RelativeLayout>  
    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:gravity="center"  
        android:orientation="vertical"  
        android:id="@+id/ly_inflate" >  
      
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="我是Java代码加载的布局" />  
      
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="我是布局里的一个小按钮" />  
      
    </LinearLayout> 
    public class MainActivity extends Activity {  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            //获得LayoutInflater对象;  
            final LayoutInflater inflater = LayoutInflater.from(this);    
            //获得外部容器对象  
            final RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);  
            Button btnLoad = (Button) findViewById(R.id.btnLoad);  
            btnLoad.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    //加载要添加的布局对象  
                    LinearLayout ly = (LinearLayout) inflater.inflate(  
                            R.layout.inflate, null, false).findViewById(  
                            R.id.ly_inflate);  
                    //设置加载布局的大小与位置  
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(    
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
                    lp.addRule(RelativeLayout.CENTER_IN_PARENT);    
                    rly.addView(ly,lp);  
                }  
            });  
        }  
    } 
  • 相关阅读:
    .net注册iis
    hdu 1081To The Max
    hdu 1312Red and Black
    hdu 1016Prime Ring Problem
    hdu 1159Common Subsequence
    hdu 1372Knight Moves
    hdu 1686Oulipo
    hdu 1241Oil Deposits
    hdu 1171Big Event in HDU
    hdu 4006The kth great number
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910762.html
Copyright © 2011-2022 走看看