充气VS findViewById [英] inflate vs findViewById
问题描述
假设我有类似如下的简单的布局XML:
button.xml:
<按钮
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / button01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
是否有下列要求的任何区别吗?哪一个我应该使用?
按钮=(按钮)getLayoutInflater()膨胀(R.layout.button,空)。
和
视图V = getLayoutInflater()膨胀(R.layout.button,空)。
按钮=(按钮)v.findViewById(R.id.button01);
这一次创建了其中R.layout.button是由xml文件button.xml的名义产生的特定布局的新视角。每次调用.inflate(...),你会得到一个新的实例。
视图V = getLayoutInflater()膨胀(R.layout.button,空)。
-
虽然是一个发现,其中R.id.button01由ID名称机器人:ID =@ + ID / button01'生成的布局在现有视图。每次调用.findViewById(R.id.button01),您将得到相同的实例,因为该视图'V'是一样的。
按钮=(按钮)v.findViewById(R.id.button01);
Suppose I have a simple layout xml like the following:
button.xml:
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Are there any differences in the following calls? and which one should i use?
button = (Button) getLayoutInflater().inflate(R.layout.button, null);
and
View v = getLayoutInflater().inflate(R.layout.button, null);
button = (Button) v.findViewById(R.id.button01);
This one creates a new view with the given layout where 'R.layout.button' is generated by the name of the xml file 'button.xml'. Each time you call .inflate(...) you will get a new instance.
View v = getLayoutInflater().inflate(R.layout.button, null);
--
While that one finds an existing view within a layout where R.id.button01 is generated by the id name 'android:id="@+id/button01"'. Every time you call .findViewById(R.id.button01) you will get the same instance since the view 'v' will be the same.
button = (Button) v.findViewById(R.id.button01);
这篇关于充气VS findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!