问题描述
我已经有严重的麻烦LayoutInflater按预期方式工作,所以没有其他人(help-for-using-layoutinflator-to-add-views-at-runtime).
I've had severe trouble getting LayoutInflater to work as expected, and so did other people (help-for-using-layoutinflator-to-add-views-at-runtime).
反复出现的问题是:为什么LayoutInflater忽略我指定的布局参数?例如。为什么从我的资源,XML的layout_width和layout_height值不兑现?
The recurring question is: why does LayoutInflater ignore the layout parameters I've specified? E.g. why are the layout_width and layout_height values from my resources XML not honored?
推荐答案
我已经研究过此问题,指的,并设立一个小样本的示范项目。以下教程演示了如何使用动态填充布局 LayoutInflater
。
I've investigated this issue, referring to the LayoutInflater docs and setting up a small sample demonstration project. The following tutorials shows how to dynamically populate a layout using LayoutInflater
.
在我们开始看到 LayoutInflater.inflate()
参数如下:
Before we get started see what LayoutInflater.inflate()
parameters look like:
- 资源:ID为一个XML布局资源加载(例如,
R.layout.main_page
) - 根:可选的观点是产生层次的父(如
attachToRoot
是真
),否则只是一个对象,它提供了一套的LayoutParams
值返回的层次结构的根(如attachToRoot
是假
) -
attachToRoot :是否充气层次应附加到根参数?如果为假,根仅用于创建
的LayoutParams
在XML根认为正确的子类。
- 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
istrue
), or else simply an object that provides a set ofLayoutParams
values for root of the returned hierarchy (ifattachToRoot
isfalse
.) 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.
返回:充气层次的根视图。如果根供给和 attachToRoot
是真
,这是根本;否则它是膨胀的XML文件的根。
Returns: The root View of the inflated hierarchy. If root was supplied and attachToRoot
is true
, this is root; otherwise it is the root of the inflated XML file.
现在的样本布局和code。
Now for the sample layout and code.
主要布局(的main.xml
):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
加入到这个容器是一个单独的TextView的,因为红色的小广场看到,如果布局参数是从XML成功应用( red.xml
):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#ff0000"
android:text="red" />
现在 LayoutInflater
用于与调用参数的几个变化
Now LayoutInflater
is used with several variations of call parameters
public class InflaterTest extends Activity {
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
// result: layout_height=wrap_content layout_width=match_parent
view = LayoutInflater.from(this).inflate(R.layout.red, null);
parent.addView(view);
// result: layout_height=100 layout_width=100
view = LayoutInflater.from(this).inflate(R.layout.red, null);
parent.addView(view, 100, 100);
// result: layout_height=25dp layout_width=25dp
// view=textView due to attachRoot=false
view = LayoutInflater.from(this).inflate(R.layout.red, parent, false);
parent.addView(view);
// result: layout_height=25dp layout_width=25dp
// parent.addView not necessary as this is already done by attachRoot=true
// view=root due to parent supplied as hierarchy root and attachRoot=true
view = LayoutInflater.from(this).inflate(R.layout.red, parent, true);
}
}
参数变化的实际结果记录在code。
The actual results of the parameter variations are documented in the code.
简介:电话 LayoutInflater
不指定根导致膨胀呼叫忽略从XML布局参数。调用膨胀时根本不等于空
和 attachRoot = TRUE
并加载布局参数,但再次返回根对象,其中prevents进一步布局更改加载的对象(除非你能找到它使用 findViewById()
)。因此,调用约定,你最有可能想用的是这个:
SYNOPSIS: Calling LayoutInflater
without specifying root leads to inflate call ignoring the layout parameters from the XML. Calling inflate with root not equal null
and attachRoot=true
does load the layout parameters, but returns the root object again, which prevents further layout changes to the loaded object (unless you can find it using findViewById()
).The calling convention you most likely would like to use is therefore this one:
loadedView = LayoutInflater.from(context)
.inflate(R.layout.layout_to_load, parent, false);
要帮助布局问题,href="http://developer.android.com/guide/developing/tools/hierarchy-viewer.html">层次观众强烈推荐
To help with layout issues, the hierarchy viewer is highly recommended.
这篇关于制作LayoutInflater感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!