• 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);  
                }  
            });  
        }  
    } 
  • 相关阅读:
    多线程的互斥(下)——信号量
    Linux下几种另类创建文件之方法
    Linux下VsFTP和ProFTP用户管理高级技巧 之一
    全面了解Linux下Proc文件系统
    许可证大阅兵
    SSH远程快速登录Linux
    为root账户更名
    Linux集群的I/O性能测试
    Think Pad笔记本分区解决思路及方法
    图形界面备份Linux系统介绍
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910762.html
Copyright © 2020-2023  润新知