android 中 setContentView() 的前世今生

新建一个android项目时, 初始化activity时,总能看到到setContentView(R.layout.main);这行代码,以前只知道这行代码能把main布局文件的内容布局加载并显示出来,但是加载的过程并不是十分清楚,今天经过查询资料,对他的实现过程总结一下,自己学习的同时也希望能帮助有相同疑惑的同仁们。

android的编写与java有着莫大的渊源,我们从java一路走来,习惯于任何对象都是new出来,android与java一脉相承,那么在这里面我们需要的对象可以自己new吗?

回答是肯定的,下面的控件都是通过硬编码new出所需的控件,

 LayoutParams params=new LayoutParams(0,LayoutParams.WRAP_CONTENT, 1.0f);//注意是那个布局的布局参数,如线性布局
       
       TextView tvUserName=new TextView(this);
       tvUserName.setText("用户名");
       EditText etUserName=new EditText(this);
       etUserName.setHint("请输入用户名");
       etUserName.setLayoutParams(params);//设置布局参数
       LinearLayout row1=new LinearLayout(this);
       row1.addView(tvUserName);
       row1.addView(etUserName);
       
       TextView tvPassword=new TextView(this);
       tvPassword.setText("密	码");
       EditText etPassword=new EditText(this);
       etPassword.setHint("请输入密码");
       LinearLayout row2=new LinearLayout(this);
       row2.addView(tvPassword);
       row2.addView(etPassword,params);
       
       
       Button btnLogin=new Button(this);
       btnLogin.setText("登陆");
       Button btnCancle=new Button(this);
       btnCancle.setText("取消");
       LinearLayout row3=new LinearLayout(this);
       row3.addView(btnLogin,params);
       row3.addView(btnCancle,params);
       
       LinearLayout root=new LinearLayout(this);
       root.setOrientation(LinearLayout.VERTICAL);
       root.addView(row1);
       root.addView(row2);
       root.addView(row3);
       this.setContentView(root);
我们并把多有的组件组成视图,然后单击运行,出现下面的界面:
这说明采用硬编码的方式完全可以实现将布局文件文件显示出来,但是采用这种方式显示,需要书写大量的代码,增加程序员的负担,因此,android把布局放到xml文件中,
作为资源的一部分,通过LayoutInflater这个类将xml布局文件加载并显示成对应视图。解析xml文件需要用到的方法有LayoutInflater的静态方法from来获取一个LayoutInflater实例inflater,再通过inflater的inflate方法返回一个视图实例,最后通过setContentView的方法把这个视图实例加载出来。实现的代码如下:
对应的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名" />

        <EditText
            android:id="@+id/etUserName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码" />

        <EditText
            android:id="@+id/etUserPass"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="登    录" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="取    消" />
    </LinearLayout>

</LinearLayout>

在activity中用到的相关代码:
 

 
 
	LayoutInflater inflater=LayoutInflater.from(this);
   	LinearLayout root= (LinearLayout) inflater.inflate(R.layout.main, null);
   this.setContentView(root);
LayoutInflater这个类一般用在非activity中设置相应的视图;
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值