加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Android开发教程:LayoutInflater的实施

发布时间:2021-11-24 15:58:12 所属栏目:PHP教程 来源:互联网
导读:LayoutInflater在Android中是扩展的意思,作用类似findViewById( ),它在Android开发中的作用是很大的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。 LayoutInflater与findViewById( )的不同点: LayoutInflater是将XML中
LayoutInflater在Android中是“扩展”的意思,作用类似findViewById( ),它在Android开发中的作用是很大的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。
 
LayoutInflater与findViewById( )的不同点:
 
LayoutInflater是将XML中的Layout转换为View放入.java代码中
findViewById()是找具体xml下的具体组件(如:Button,TextView,ImageView等)。
获得LayoutInflater的三种方法:
 
第一种:
 
LayoutInflater inflater = LayoutInflater.from(this);  
View layout = inflater.inflate(R.layout.main, null);
第二种:
 
LayoutInflater inflater = getLayoutInflater();  
View layout = inflater.inflate(R.layout.main, null);
第三种:
 
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
View layout = inflater.inflate(R.layout.main, null);
getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入 的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。其中LAYOUT_INFLATER_SERVICE返回的对象是 LayoutInflater,作用是取得XML定义的View。
 
第一个实例:
 
main.xml
 
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button   
    android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button   
    android:id="@+id/btn2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>
MainActivity.java
 
package com.lingdududu.test;  
 
import android.app.Activity;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
 
public class MainActivity extends Activity {  
    private EditText etx;  
    private Button confirmBtn;  
    private Button cancleBtn;  
      
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
          
        LayoutInflater inflater = getLayoutInflater();  
        View layout = inflater.inflate(R.layout.main, null);  
          
        etx = (EditText)layout.findViewById(R.id.text);  
        etx.setBackgroundColor(Color.WHITE);  
        etx.setHint("请输入你的学号");  
          
        confirmBtn = (Button)layout.findViewById(R.id.btn1);  
        confirmBtn.setText("确定");  
          
        cancleBtn = (Button)layout.findViewById(R.id.btn2);  
        cancleBtn.setText("取消");  
          
        setContentView(layout);     
    }  


(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读