public abstract class LayoutInflater extends Object
View
objects. It is never used directly. Instead, use
Activity.getLayoutInflater()
or
Context.getSystemService(java.lang.String)
to retrieve a standard LayoutInflater instance
that is already hooked up to the current context and correctly configured
for the device you are running on. For example:
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
To create a new LayoutInflater with an additional LayoutInflater.Factory
for your
own views, you can use cloneInContext(android.content.Context)
to clone an existing
ViewFactory, and then call setFactory(android.view.LayoutInflater.Factory)
on it to include your
Factory.
For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
Modifier and Type | Class and Description |
---|---|
static interface |
LayoutInflater.Factory |
static interface |
LayoutInflater.Factory2 |
static interface |
LayoutInflater.Filter
Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed
to be inflated.
|
Modifier and Type | Field and Description |
---|---|
protected Context |
mContext
This field should be made private, so it is hidden from the SDK.
|
Modifier | Constructor and Description |
---|---|
protected |
LayoutInflater(Context context)
Create a new LayoutInflater instance associated with a particular Context.
|
protected |
LayoutInflater(LayoutInflater original,
Context newContext)
Create a new LayoutInflater instance that is a copy of an existing
LayoutInflater, optionally with its Context changed.
|
Modifier and Type | Method and Description |
---|---|
abstract LayoutInflater |
cloneInContext(Context newContext)
Create a copy of the existing LayoutInflater object, with the copy
pointing to a different Context than the original.
|
View |
createView(String name,
String prefix,
AttributeSet attrs)
Low-level function for instantiating a view by name.
|
static LayoutInflater |
from(Context context)
Obtains the LayoutInflater from the given context.
|
Context |
getContext()
Return the context we are running in, for access to resources, class
loader, etc.
|
LayoutInflater.Factory |
getFactory()
Return the current
LayoutInflater.Factory (or null). |
LayoutInflater.Factory2 |
getFactory2()
Return the current
LayoutInflater.Factory2 . |
LayoutInflater.Filter |
getFilter() |
View |
inflate(int resource,
ViewGroup root)
Inflate a new view hierarchy from the specified xml resource.
|
View |
inflate(int resource,
ViewGroup root,
boolean attachToRoot)
Inflate a new view hierarchy from the specified xml resource.
|
View |
inflate(XmlPullParser parser,
ViewGroup root)
Inflate a new view hierarchy from the specified xml node.
|
View |
inflate(XmlPullParser parser,
ViewGroup root,
boolean attachToRoot)
Inflate a new view hierarchy from the specified XML node.
|
protected View |
onCreateView(String name,
AttributeSet attrs)
This routine is responsible for creating the correct subclass of View
given the xml element name.
|
protected View |
onCreateView(View parent,
String name,
AttributeSet attrs)
Version of
onCreateView(String, AttributeSet) that also
takes the future parent of the view being constructed. |
void |
setFactory(LayoutInflater.Factory factory)
Attach a custom Factory interface for creating views while using
this LayoutInflater.
|
void |
setFactory2(LayoutInflater.Factory2 factory)
Like
setFactory(android.view.LayoutInflater.Factory) , but allows you to set a LayoutInflater.Factory2
interface. |
void |
setFilter(LayoutInflater.Filter filter)
Sets the
LayoutInflater.Filter to by this LayoutInflater. |
void |
setPrivateFactory(LayoutInflater.Factory2 factory) |
protected final Context mContext
protected LayoutInflater(Context context)
Context.getSystemService()
to retrieve
the standard Context.INFLATER_SERVICE
.context
- The Context in which this LayoutInflater will create its
Views; most importantly, this supplies the theme from which the default
values for their attributes are retrieved.protected LayoutInflater(LayoutInflater original, Context newContext)
cloneInContext(android.content.Context)
.original
- The original LayoutInflater to copy.newContext
- The new Context to use.public static LayoutInflater from(Context context)
public abstract LayoutInflater cloneInContext(Context newContext)
ContextThemeWrapper
to create a new LayoutInflater to go along
with the new Context theme.newContext
- The new Context to associate with the new LayoutInflater.
May be the same as the original Context if desired.public Context getContext()
public final LayoutInflater.Factory getFactory()
LayoutInflater.Factory
(or null). This is called on each element
name. If the factory returns a View, add that to the hierarchy. If it
returns null, proceed to call onCreateView(name).public final LayoutInflater.Factory2 getFactory2()
LayoutInflater.Factory2
. Returns null if no factory is set
or the set factory does not implement the LayoutInflater.Factory2
interface.
This is called on each element
name. If the factory returns a View, add that to the hierarchy. If it
returns null, proceed to call onCreateView(name).public void setFactory(LayoutInflater.Factory factory)
onCreateView(java.lang.String, android.util.AttributeSet)
method is called.
If you have an existing
LayoutInflater and want to add your own factory to it, use
cloneInContext(android.content.Context)
to clone the existing instance and then you
can use this function (once) on the returned new instance. This will
merge your own factory with whatever factory the original instance is
using.
public void setFactory2(LayoutInflater.Factory2 factory)
setFactory(android.view.LayoutInflater.Factory)
, but allows you to set a LayoutInflater.Factory2
interface.public void setPrivateFactory(LayoutInflater.Factory2 factory)
public LayoutInflater.Filter getFilter()
LayoutInflater.Filter
currently used by this LayoutInflater to restrict the set of Views
that are allowed to be inflated.public void setFilter(LayoutInflater.Filter filter)
LayoutInflater.Filter
to by this LayoutInflater. If a view is attempted to be inflated
which is not allowed by the LayoutInflater.Filter
, the inflate(int, ViewGroup)
call will
throw an InflateException
. This filter will replace any previous filter set on this
LayoutInflater.filter
- The Filter which restricts the set of Views that are allowed to be inflated.
This filter will replace any previous filter set on this LayoutInflater.public View inflate(@LayoutRes int resource, ViewGroup root)
InflateException
if there is an error.resource
- ID for an XML layout resource to load (e.g.,
R.layout.main_page
)root
- Optional view to be the parent of the generated hierarchy.public View inflate(XmlPullParser parser, ViewGroup root)
InflateException
if there is an error. *
Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
parser
- XML dom node containing the description of the view
hierarchy.root
- Optional view to be the parent of the generated hierarchy.public View inflate(@LayoutRes int resource, ViewGroup root, boolean attachToRoot)
InflateException
if there is an error.resource
- ID for an XML layout resource to load (e.g.,
R.layout.main_page
)root
- Optional view to be the parent of the generated hierarchy (if
attachToRoot is true), or else simply an object that
provides a set of LayoutParams values for root of the returned
hierarchy (if attachToRoot is false.)attachToRoot
- Whether the inflated hierarchy should be attached to
the root parameter? If false, root is only used to create the
correct subclass of LayoutParams for the root view in the XML.public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
InflateException
if there is an error.
Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
parser
- XML dom node containing the description of the view
hierarchy.root
- Optional view to be the parent of the generated hierarchy (if
attachToRoot is true), or else simply an object that
provides a set of LayoutParams values for root of the returned
hierarchy (if attachToRoot is false.)attachToRoot
- Whether the inflated hierarchy should be attached to
the root parameter? If false, root is only used to create the
correct subclass of LayoutParams for the root view in the XML.public final View createView(String name, String prefix, AttributeSet attrs) throws ClassNotFoundException, InflateException
There are two things that can happen in an error case: either the exception describing the error will be thrown, or a null will be returned. You must deal with both possibilities -- the former will happen the first time createView() is called for a class of a particular name, the latter every time there-after for that class name.
name
- The full name of the class to be instantiated.attrs
- The XML attributes supplied for this instance.ClassNotFoundException
InflateException
protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException
name
- The fully qualified class name of the View to be create.attrs
- An AttributeSet of attributes to apply to the View.ClassNotFoundException
protected View onCreateView(View parent, String name, AttributeSet attrs) throws ClassNotFoundException
onCreateView(String, AttributeSet)
that also
takes the future parent of the view being constructed. The default
implementation simply calls onCreateView(String, AttributeSet)
.parent
- The future parent of the returned view. Note that
this may be null.name
- The fully qualified class name of the View to be create.attrs
- An AttributeSet of attributes to apply to the View.ClassNotFoundException