为什么 LayoutInflater 会忽略我指定的 layout_width 和 layout_height 布局参数?
我在让 LayoutInflater 按预期工作时遇到了严重的问题,其他人也是如此:如何使用 layoutinflator 在运行时添加视图?.
I've had severe trouble getting LayoutInflater to work as expected, and so did other people: How to use layoutinflator to add views at runtime?.
为什么 LayoutInflater 会忽略我指定的布局参数?例如.为什么我的资源 XML 中的 layout_width
和 layout_height
值不受尊重?
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 docs 并设置一个小示例演示项目.以下教程展示了如何使用 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:
-
resource:要加载的 XML 布局资源的 ID(例如,
R.layout.main_page
) -
root:作为生成层次结构的父级的可选视图(如果
attachToRoot
是true
),或者只是一个提供返回层次结构的根的一组LayoutParams
值(如果attachToRoot
是false
.) attachToRoot:膨胀的层次结构是否应该附加到根参数上?如果为 false,则 root 仅用于为 XML 中的根视图创建
LayoutParams
的正确子类.
-
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.
返回:膨胀层次结构的根视图.如果提供了 root 并且 attachToRoot
是 true
,则这是 root;否则它是膨胀的 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.
现在是示例布局和代码.
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
) 成功应用,则可见为红色小方块:
Added into this container is a separate TextView, visible as small red square if layout parameters are successfully applied from 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);
}
}
参数变化的实际结果记录在代码中.
The actual results of the parameter variations are documented in the code.
概要: 调用 LayoutInflater
而不指定 root 会导致 inflate 调用忽略来自 XML 的布局参数.在 root 不等于 null
和 attachRoot=true
的情况下调用 inflate 确实会加载布局参数,但会再次返回根对象,这会阻止对加载对象的进一步布局更改(除非您可以使用 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);
为了帮助解决布局问题,强烈建议使用布局检查器.
To help with layout issues, the Layout Inspector is highly recommended.