inflate函数及其使用例子 笔记



LayoutInflater的inflate函数用法详解

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。获取LayoutInflater的方法有如下三种

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)
View layout = inflater.inflate(R.layout.main, null);

setContentView inflate 的区别

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

public View inflate(int Resourece,ViewGroup root)
作用:填充一个新的视图层次结构从指定的
XML资源文件中
reSource
ViewlayoutID
root
 生成的层次结构的根视图
return 
填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

其余几个重载的inflate函数类似。



例子:

1.普通Activity中使用inflate函数1
View view = View.inflate(this, R.layout.splash_animation, null);
//splash_animation.xml file
setContentView(view);


2.普通Activity中使用inflate函数2
View selectView = getLayoutInflater().inflate(R.layout.list, null);

final ListView listview = (ListView)(selectView.findViewById(R.id.list));
new AlertDialog.Builder(BlockList.this).setView(selectView).setPositiveButton("OK", new DialogInterface.OnClickListener(){
});


3.在Fragment类的onCreateView中利用形参中的LayoutInflator inflater
public class AuctionListFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater
, ViewGroup container, Bundle savedInstanceState)
{
// 加载/res/layout/目录下的function_list.xml布局文件
View rootView = inflater.inflate(R.layout.auction_list,
container, false);
auctionList = (ListView) rootView.findViewById(
R.id.auction_list);


return rootView;
}
}


4.在Fragment类的自定义函数中使用 getActivity().getLayoutInflater() .inflate()....

public class ViewItemFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater
, ViewGroup container, Bundle savedInstanceState)
{

}


private void viewItemDetail(int position)
{

// 加载detail.xml界面布局代表的视图
View detailView = getActivity().getLayoutInflater()
.inflate(R.layout.detail, null);


}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用zlib库中的inflate函数解压2MB大小的字节数组,您可以按照以下步骤进行操作: 1. 定义一个z_stream结构体,用于存储解压缩所需的状态信息: ``` z_stream stream; ``` 2. 初始化z_stream结构体,设置zalloc、zfree和opaque字段: ``` stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; ``` 3. 调用inflateInit函数初始化z_stream结构体,准备进行解压缩: ``` int ret = inflateInit(&stream); if (ret != Z_OK) { // 初始化失败,处理错误 } ``` 4. 定义一个输入缓冲区input_buf和一个输出缓冲区output_buf,读入2MB大小的字节数组到input_buf中: ``` unsigned char input_buf[2 * 1024 * 1024]; // 读入2MB大小的字节数组到input_buf中 // ... ``` 5. 定义一个变量have_bytes表示已经解压的字节数,以及一个变量total_bytes表示总共需要解压的字节数,将have_bytes初始化为0,total_bytes初始化为2MB: ``` int have_bytes = 0; int total_bytes = 2 * 1024 * 1024; ``` 6. 在一个循环中,不断调用inflate函数解压数据,直到所有数据解压完成: ``` while (have_bytes < total_bytes) { // 设置输入缓冲区的位置和可用大小 stream.next_in = input_buf + have_bytes; stream.avail_in = total_bytes - have_bytes; // 设置输出缓冲区的位置和可用大小 stream.next_out = output_buf; stream.avail_out = sizeof(output_buf); // 调用inflate函数解压数据 int ret = inflate(&stream, Z_NO_FLUSH); if (ret == Z_STREAM_ERROR) { // 解压失败,处理错误 } else if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) { // 解压失败,处理错误 } // 更新已经解压的字节数 have_bytes += sizeof(output_buf) - stream.avail_out; // 判断是否已经解压完成 if (stream.avail_out > 0) { break; } } ``` 7. 在解压完成后,调用inflateEnd函数释放z_stream结构体所占用的资源: ``` inflateEnd(&stream); ``` 以上是使用zlib库中的inflate函数解压2MB大小的字节数组的基本步骤,您可以根据实际情况进行调整。注意,为了保证解压效率,您可以将输入缓冲区和输出缓冲区的大小调整为适当的值,比如4KB或8KB,以便在保证解压正确性的前提下,尽可能地提高解压速度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值