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);  
                }  
            });  
        }  
    } 
  • 相关阅读:
    【数据结构栈应用系列】括号匹配
    【二叉树系列】二叉树课程大作业
    Tomcat在Linux上的安装与配置
    索引介绍及创建与删除索引
    Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
    Tomcat的配置文件server.xml叙述
    问题及解决方法
    Oracle DB 查看预警日志
    报表简介
    Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910762.html
Copyright © 2011-2022 走看看