从 XML 文件添加的视图未显示在其父视图中
View added from XML file is not shown in its parent view
在我的 Player activity 中,我试图通过调用 addView
:[=29= 添加来自不同 XML 的视图]
view = (ViewGroup)findViewById(R.id.player_ConstraintLayout);
notifierView = LayoutInflater.from(view.getRootView().getContext())
.inflate(R.layout.notifier, null);
view.addView(notifierView);
// view.invalidate();
Player activity 附带此关联的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/player_ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.Player">
...
</android.support.constraint.ConstraintLayout>
而我要添加的是 notifier.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:translationZ="9dp"
android:elevation="9dp">
</android.support.constraint.ConstraintLayout>
我已经调试了应用程序并通过检查变量 view
我可以确认 notifierView
已添加并且在 view
的子列表中。但是 notifierView
没有显示,即使我期望它是全屏大小和红色。当我在调试器中检查 notifierView
时,它显示其 mTop
、mBottom
、mLeft
和 mRight
变量均为 0
。我假设这意味着
android:layout_width="match_parent"
android:layout_height="match_parent"
被忽略了?还是有其他原因导致我根本看不到这个 notifierView
?
EDIT/UPDATE:
让我向您展示更多代码,因为上面的代码稍微简化了。
在 Player activity 我有:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
notifier = new Notifier((ViewGroup)findViewById(R.id.player_ConstraintLayout));
...
}
在Notifier.java中我有
Notifier(ViewGroup view) {
handler = new android.os.Handler();
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
}
ConstraintLayout
实际上不支持 match_parent
因为它的 children。您必须将此用于 "match parent" 宽度:
android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
而这个 "match parent" 身高:
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
此外,您的 LayoutInflater.inflate()
调用应该更改。当您将 null
作为第二个参数传递时,您没有为系统提供足够的信息来理解如何解析膨胀视图上的 layout_
属性;您应该传递一个真实的 parent(在本例中为 view
)。但是,如果将 null
更改为 view
,则 inflate()
调用的 return 值将发生变化,因此您必须添加一个额外的参数 (attachToRoot
)确保其余语义不变。
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
(代表问题作者发布解决方案).
无效缓存/重新启动 帮助。 Android Studio 有时会出错。
在我的 Player activity 中,我试图通过调用 addView
:[=29= 添加来自不同 XML 的视图]
view = (ViewGroup)findViewById(R.id.player_ConstraintLayout);
notifierView = LayoutInflater.from(view.getRootView().getContext())
.inflate(R.layout.notifier, null);
view.addView(notifierView);
// view.invalidate();
Player activity 附带此关联的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/player_ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.Player">
...
</android.support.constraint.ConstraintLayout>
而我要添加的是 notifier.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:translationZ="9dp"
android:elevation="9dp">
</android.support.constraint.ConstraintLayout>
我已经调试了应用程序并通过检查变量 view
我可以确认 notifierView
已添加并且在 view
的子列表中。但是 notifierView
没有显示,即使我期望它是全屏大小和红色。当我在调试器中检查 notifierView
时,它显示其 mTop
、mBottom
、mLeft
和 mRight
变量均为 0
。我假设这意味着
android:layout_width="match_parent"
android:layout_height="match_parent"
被忽略了?还是有其他原因导致我根本看不到这个 notifierView
?
EDIT/UPDATE:
让我向您展示更多代码,因为上面的代码稍微简化了。
在 Player activity 我有:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
notifier = new Notifier((ViewGroup)findViewById(R.id.player_ConstraintLayout));
...
}
在Notifier.java中我有
Notifier(ViewGroup view) {
handler = new android.os.Handler();
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
}
ConstraintLayout
实际上不支持 match_parent
因为它的 children。您必须将此用于 "match parent" 宽度:
android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
而这个 "match parent" 身高:
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
此外,您的 LayoutInflater.inflate()
调用应该更改。当您将 null
作为第二个参数传递时,您没有为系统提供足够的信息来理解如何解析膨胀视图上的 layout_
属性;您应该传递一个真实的 parent(在本例中为 view
)。但是,如果将 null
更改为 view
,则 inflate()
调用的 return 值将发生变化,因此您必须添加一个额外的参数 (attachToRoot
)确保其余语义不变。
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
(代表问题作者发布解决方案).
无效缓存/重新启动 帮助。 Android Studio 有时会出错。