LayoutInflate的使用

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。


setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。


LayoutInflater在Android中是“扩展”的意思,作用类似于findViewById(),不同的是LayoutInflater是用来获得布局文件对象的,而findViewById()是用来获得具体控件的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。


获取LayoutInflater有三种方法:

LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LayoutInflater inflate = getLayoutInflater();//在Activity中使用
LayoutInflater inflate = LayoutInflater.from(getApplicationContext());

用法举例:

user_folder.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

MainActivity.java:

package com.example.hello;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{
	private RelativeLayout frame;
	private View mySelfView1 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        frame = (RelativeLayout) findViewById(R.id.frame);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
        
		// 第一种方式
        // LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        // 第二种方式
        // LayoutInflater inflate = getLayoutInflater();
        LayoutInflater inflate = LayoutInflater.from(getApplicationContext());
        mySelfView1 = inflate.inflate(R.layout.user_folder, null);
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
        if(mySelfView1 != null){
        	Toast.makeText(getApplicationContext(), "show my selfview", Toast.LENGTH_SHORT).show();
        	frame.addView(mySelfView1);
        	
        	Handler handler = new Handler();
        	handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					frame.removeView(mySelfView1);
				}
			}, 2000);
        } else {
        	Toast.makeText(getApplicationContext(), "selfview is null", Toast.LENGTH_SHORT).show();
        }
	}
    
}





  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值