Android 中LayoutInflater的使用!

OpenAI Realtime API实时语音交互实战指南 实时语音交互是人机自然对话的核心能力,其本质在于构建低延迟、双向、流式协同的音频-语义闭环。它突破传统REST API的请求-响应范式,依托WebSocket实现毫秒级音频输入流(audio.delta)、模型生成流(text/audio.delta)与动态控制流(session.update)三重并行。该技术显著降低端到端延迟至300ms以内,支撑语音助手、智能客服、对话式教育等强实时场景。本文聚焦OpenAI Realtime API的工程落地,详解PCM音频流处理、VAD语音活动检测、AudioCon 阅读详情

来自:hhttp://blog.csdn.net/android_tutor/article/details/5513869


大家好我们这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(),

 

不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。

 

为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边ImageView,右边TextView)。

效果图如下:

 

 

下面我将详细的说明Demo的实现过程:

 

1、新建一个Android工程,我们命名为LayoutInflaterDemo.

 

2、修改main.xml布局,里面主要在原来基础上增加了一个Button.代码如下:

 

  1. <?xml version="1.0"   
  2. encoding="utf-8"?>  
  3. <LinearLayout   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent"  
  8.     >  
  9. <TextView    
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="@string/hello"  
  13.     />  
  14. <Button  
  15.     android:id="@+id/button"  
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content"  
  18.     android:text="ShowCustomDialog"  
  19.     />  
  20. </LinearLayout>  

 

3.定义对话框的布局方式,我们在layout目录下,新建一个名为custom_dialog.xml文件具体代码如下:

 

  1. <?xml version="1.0"   
  2. encoding="utf-8"?>  
  3. <LinearLayout   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5.               android:orientation="horizontal"  
  6.               android:layout_width="fill_parent"  
  7.               android:layout_height="fill_parent"  
  8.               android:padding="10dp"  
  9.               >  
  10.     <ImageView android:id="@+id/image"  
  11.                android:layout_width="wrap_content"  
  12.                android:layout_height="fill_parent"  
  13.                android:layout_marginRight="10dp"  
  14.                />  
  15.     <TextView android:id="@+id/text"  
  16.               android:layout_width="wrap_content"  
  17.               android:layout_height="fill_parent"  
  18.               android:textColor="#FFF"  
  19.               />  
  20. </LinearLayout>  

 

4.修改主程序LayouInflaterDemo.java代码如下:

 

  1. package com.android.tutor;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12. public class LayoutInflaterDemo extends Activity implements   
  13. OnClickListener {  
  14.       
  15.     private Button button;  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         button = (Button)findViewById(R.id.button);  
  21.         button.setOnClickListener(this);  
  22.     }  
  23.     @Override  
  24.     public void onClick(View v) {  
  25.           
  26.         showCustomDialog();  
  27.     }  
  28.       
  29.     public void showCustomDialog()  
  30.     {  
  31.         AlertDialog.Builder builder;  
  32.         AlertDialog alertDialog;  
  33.         Context mContext = LayoutInflaterDemo.this;  
  34.           
  35.         //下面俩种方法都可以  
  36.         ////LayoutInflater inflater = getLayoutInflater();  
  37.         LayoutInflater inflater = (LayoutInflater)   
  38. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  39.         View layout = inflater.inflate(R.layout.custom_dialog,null);  
  40.         TextView text = (TextView) layout.findViewById(R.id.text);  
  41.         text.setText("Hello, Welcome to Mr Wei's blog!");  
  42.         ImageView image = (ImageView) layout.findViewById(R.id.image);  
  43.         image.setImageResource(R.drawable.icon);  
  44.         builder = new AlertDialog.Builder(mContext);  
  45.         builder.setView(layout);  
  46.         alertDialog = builder.create();  
  47.         alertDialog.show();  
  48.     }  
  49. }  

 

5、最后执行之,点击Button,将得到上述效果。

简言之,LayoutInflater 用来将.xml布局文件转化为View类的实例

Sigrity OptimizePI Best Capacitor Location Estimation模式如何评估电容最佳位置操作指导 Sigrity OptimizePI Best Capacitor Location Estimation模式用于前仿真分析,评估电容的最佳位置以获得最小回路电感,如何分析,以下图为例进行说明。IC devices,输入U*, A*, B*, Q*, P*,将所有这些字母开头的器件都识别为IC。Min设置为1(最少电容个数为1),max设置为28(最多电容个数为28)Capacitors 输入C*,将所有C开头的器件都识别为电容。Grid X设置为8,Grid Y也设置为8。 阅读详情

相关推荐

【毕业设计】_基于STM32单片机的按摩椅( 原理图+源码)

按摩器系统基于STM32单片机设计,作为核心控制单元。系统配备了OLED显示屏、蓝牙模块、喇叭模块、播报模块和光电感应模块。光电感应器用于检测是否有人接近,同时,系统可以通过蓝牙接收控制指令并执行相应的操作。该系统支持40℃的热敷功能以及气囊按摩功能,具备温度显示功能,并且可以通过按键切换不同的按摩模式。当光电感应器检测到有人时,按摩椅会自动启动。使用DHT11传感器的红色按钮可以调整温度。

2401_85381100的博客 728

【LayoutInflater】Android 中LayoutInflater的使用!

http://weizhulin.blog.51cto.com/1556324/311450 大家好我们这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(), 不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是

horsttnann的专栏 345

USB接口ESD保护电路:深度剖析与选型建议

USB接口在热插拔和频繁使用中极易遭遇静电放电(ESD)冲击,导致芯片损坏或通信异常。本文结合实际电路设计经验,详解TVS二极管、集成ESD防护器件等方案在USB接口上的应用要点,重点分析钳位电压、结电容、IEC 61000-4-2等级等关键参数对信号完整性与防护效果的影响,助你为USB接口匹配高可靠、低干扰的ESD保护方案。

weixin_36289813的博客 519

Android高手进阶教程(五)之----Android 中LayoutInflater的使用!

大家好我们这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(), 不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。 为了让大家容

Android_Tutor的专栏 2万+

医疗影像分割 | 使用MedSAM训练自己的医学图像分割数据集

摘要 本文详细介绍了使用MedSAM模型训练自定义医学图像数据集的完整流程。主要内容包括:代码下载与环境配置(创建虚拟环境、安装依赖、验证CUDA)、数据集准备(目录结构、格式要求、命名规范及检查工具)、预训练模型下载(不同模型选择建议),以及训练脚本修改(解决文件名匹配、Windows系统NCCL支持、单GPU训练等常见问题)。通过逐步指导,帮助用户快速搭建MedSAM训练环境并适配自己的医学图像数据,实现高效的分割模型训练。

LiRongLu_的博客 495

RISC-V CPU设计(三)---多周期CPU的设计与实现

基本情况 本多周期CPU支持RV32I指令集(fence,ecall,ebreak除外),已通过部分指令的测试,如代码有bug,欢迎指出。 模块划分 代码将CPU划分为以下几个模块: 1.控制单元:ControlUnit 用于产生控制信号,是CPU的大脑,指挥中枢。 2.分支决策单元:BranchUnit 用于决定B型指令是否发生跳转 3.立即数单元:ImmUnit 用于从指令中解析出立即数. 4.寄存器堆:RegisterFile RISC-V的32个寄存器,其中x0寄存器恒为0,不可更改。 5.指令存储

qq_40268672的博客 5550
上一篇: Android 中自定义属性(attr.xml,TypedArray)的使用!
下一篇: Android 中MenuInflater的使用(布局定义菜单)!
shareCode
博客等级 码龄15年 125粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址