LayoutInflater的用法总结

在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater。
LayoutInflater在Android中是“扩展”的意思,作用类似于findViewById(),不同的是LayoutInflater是用来获得布局文件对象的,而

findViewById()是用来获得具体控件的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。
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);

第一种方法的本质就是调用第三种方法,而第二种方法和第三种方法有什么区别呢?

在第二种getLayoutInflater源代码里,就是调用第三种,所以第二和第三是一样的。

第二种方法用于Activity类,而第三种方法主要用于BaseAdapter类,由于BaseAdapter类并不像Activity类有getLayoutInflater()方法可以获得LayoutInflater对象,因此,需要使用Context类的getSystemService方法来获得LayoutInflater对象。

下面是简单的使用示例:

public class LayoutInflaterActivity extends Activity {
	private EditText et;
	private Button btn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 第一种方法
		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);
		// 这里是通过事先获得的布局文件来实例化具体控件,并且可以根据情况自定义控件
		et = (EditText) layout.findViewById(R.id.edittext);
		et.setBackgroundColor(Color.YELLOW);
		btn = (Button) layout.findViewById(R.id.btn);
		btn.setBackgroundColor(Color.CYAN);
		// 显示
		setContentView(layout);
	}
}

传入的Name                              返回的对象                          说明
WINDOW_SERVICE               WindowManager           管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater               取得xml里定义的view
ACTIVITY_SERVICE               ActivityManager            管理应用程序的系统状态
POWER_SERVICE                  PowerManger                电源的服务
ALARM_SERVICE                   AlarmManager              闹钟的服务
NOTIFICATION_SERVICE       NotificationManager      状态栏的服务
KEYGUARD_SERVICE             KeyguardManager         键盘锁的服务
LOCATION_SERVICE              LocationManager          位置的服务,如GPS
SEARCH_SERVICE                 SearchManager             搜索的服务
VEBRATOR_SERVICE             Vebrator                       手机震动的服务
CONNECTIVITY_SERVICE      Connectivity                  网络连接的服务
WIFI_SERVICE                      WifiManager                  Wi-Fi服务
TELEPHONY_SERVICE            TeleponyManager         电话服务
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Popiwindow 是一个自定义的弹窗控件,可以通过以下步骤来使用: 1. 在布局文件中添加 Popiwindow 控件: ``` <com.example.popiwindow.Popiwindow android:id="@+id/popiwindow" android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` 2. 在代码中获取 Popiwindow 实例并设置弹窗内容: ``` Popiwindow popiwindow = findViewById(R.id.popiwindow); popiwindow.setContent("这是弹窗的内容"); ``` 3. 在需要弹出弹窗的地方调用 show 方法: ``` popiwindow.show(); ``` 这样就可以在屏幕上弹出一个包含指定内容的弹窗了。 ### 回答2: PopupWindow 是 Android 中一个常用的浮动窗口控件,可以在当前界面上方显示一个自定义的弹窗窗口。下面是使用 PopupWindow 的基本步骤: 1. 创建 PopupWindow 对象:可以通过实例化 PopupWindow 类来创建一个 PopupWindow 对象。构造方法可以传入一个布局文件或者自定义的 View。 2. 设置 PopupWindow 的属性:可以设置 PopupWindow 的宽度、高度、背景色、动画效果等属性。可以使用 setWidth() 和 setHeight() 方法设置宽度和高度,setBackgroundDrawable() 方法设置背景色。 3. 设置 PopupWindow 的布局:可以通过 setContentView() 方法设置 PopupWindow 显示的布局,可以是布局文件或者自定义的 View。 4. 设置 PopupWindow 的位置:可以使用 showAtLocation() 方法或者 showAsDropDown() 方法来设置 PopupWindow 在屏幕上的位置。showAtLocation() 方法可以传入一个 View 和位置参数来指定 PopupWindow 的显示位置,showAsDropDown() 方法可以传入一个锚点 View 和偏移量参数来指定 PopupWindow 相对于锚点的位置。 5. 处理 PopupWindow 的事件:可以设置 PopupWindow 中的控件的点击事件,例如按钮的点击事件。 6. 关闭 PopupWindow:可以使用 dismiss() 方法关闭 PopupWindow,当不需要显示 PopupWindow 时需要手动调用该方法。 总结:使用 PopupWindow 需要先创建一个 PopupWindow 对象,设置其属性、布局和位置,然后处理控件的事件,最后关闭 PopupWindow。注意在设置位置时需要考虑屏幕的大小和显示的效果。希望这个简单的使用步骤能帮助您理解如何使用 PopupWindow。 ### 回答3: 使用PopupWindow,首先需要创建一个PopupWindow对象,可以通过构造方法或者静态方法获取一个实例。创建PopupWindow对象时需要指定宽度、高度、背景等属性。 接下来,可以设置PopupWindow的内容布局,可以使用布局文件或者通过代码动态创建View对象。如果使用布局文件,可以通过LayoutInflater类的inflate()方法将布局文件转换为View对象。 接着,可以通过PopupWindow对象的setContentView()方法设置内容布局。 然后,可以设置PopupWindow的显示位置,可以通过指定Gravity属性来设置,在屏幕上方、下方、左侧、右侧等等。也可以通过设置offsetX和offsetY来偏移显示位置。 之后,可以设置PopupWindow的动画效果,可以通过设置setAnimationStyle()方法来设置进入和退出动画效果。 最后,可以通过调用PopupWindow的showAsDropDown()或者showAtLocation()方法来显示PopupWindow。showAsDropDown()方法可以将PopupWindow显示在指定的View下方,showAtLocation()方法则可以自由设置显示位置。 同时,还可以通过PopupWindow的一些其他方法来设置点击外部区域是否消失、设置背景等属性。 使用完PopupWindow后,可以通过PopupWindow的dismiss()方法来关闭PopupWindow。 总结就是:创建PopupWindow对象,设置内容布局,设置显示位置、动画效果、其他属性等,然后显示PopupWindow,最后关闭PopupWindow。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值