Java 从XML文件添加的视图未显示在其父视图中
在我的播放器活动中,我试图通过调用Java 从XML文件添加的视图未显示在其父视图中,java,android,android-layout,android-studio,android-view,Java,Android,Android Layout,Android Studio,Android View,在我的播放器活动中,我试图通过调用addView从不同的XML添加视图: view = (ViewGroup)findViewById(R.id.player_ConstraintLayout); notifierView = LayoutInflater.from(view.getRootView().getContext()) .inflate(R.layout.notifier, null); vie
addView
从不同的XML添加视图:
view = (ViewGroup)findViewById(R.id.player_ConstraintLayout);
notifierView = LayoutInflater.from(view.getRootView().getContext())
.inflate(R.layout.notifier, null);
view.addView(notifierView);
// view.invalidate();
播放器活动附带以下关联的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>
你被忽视了吗?或者我根本看不到这个notifierView
还有其他原因吗
编辑/更新: 让我向您展示更多的代码,因为上面的代码有点简化 在玩家活动中,我有:
@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中,我有
ConstraintLayout
实际上不支持其子项的match\u parent
。对于“匹配父项”宽度,必须使用此选项:
对于“匹配父项”高度:
此外,应该更改您的LayoutInflater.inflate()
调用。当您将null
作为第二个参数传递时,您没有为系统提供足够的信息来理解如何解析膨胀视图上的layout\uu
属性;您应该传递一个真实的父对象(在本例中为view
)。但是,如果将null
更改为view
,则inflate()
调用的返回值将更改,因此必须添加一个额外的参数(attachToRoot
)以确保其余语义不会更改
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
ConstraintLayout
实际上不支持其子项的match\u parent
。对于“匹配父项”宽度,必须使用此选项:
对于“匹配父项”高度:
此外,应该更改您的LayoutInflater.inflate()
调用。当您将null
作为第二个参数传递时,您没有为系统提供足够的信息来理解如何解析膨胀视图上的layout\uu
属性;您应该传递一个真实的父对象(在本例中为view
)。但是,如果将null
更改为view
,则inflate()
调用的返回值将更改,因此必须添加一个额外的参数(attachToRoot
)以确保其余语义不会更改
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
(代表问题作者发布解决方案)
无效缓存/重新启动帮助。Android Studio有时会出错。(代表问题作者发布解决方案)
无效缓存/重新启动帮助。Android Studio有时会出错。首先,我相信
ConstraintLayout
确实为其子代支持match\u parent
。但我还是用了你的建议和代码。不幸的是,两者没有区别。它未显示,并且mLeft
、mTop
、mRight
和mButtom
仍为0
@Ω这些文件似乎与我的意见一致。搜索“将大小设置为一个比率”,并查看其上方。注意:不能对ConstraintLayout中的任何视图使用match_parent。请改为使用“match constraints”(0dp)@Ωmega您是否可能在问题中遗漏了相关代码?在我用一个非常基本的布局进行测试时,我发布的内容很好,但也只是使用默认的布局参数就可以提供匹配/匹配维度。当你构建一个简单的应用程序时,它的父级(透明)内部有ConstraintLayout
并设置android:layout\u width=“match\u parent”
和android:layout\u height=“match\u parent”
这两个版本,在子版布局上加上一些背景色,你会看到它确实有效。一定是其他原因导致了问题。我已经编码了android:layout\u width=“50dp”
和android:layout\u height=“50dp
以查看是否会得到一些东西,但不幸的是,没有显示任何内容,所有坐标仍然是0
。首先,我相信ConstraintLayout
确实支持其子项的match\u parent
。但我还是用了你的建议和代码。不幸的是,两者没有区别。它未显示,并且mLeft
、mTop
、mRight
和mButtom
仍为0
@Ω这些文件似乎与我的意见一致。搜索“将大小设置为一个比率”,并查看其上方。注意:不能对ConstraintLayout中的任何视图使用match_parent。请改为使用“match constraints”(0dp)@Ωmega您是否可能在问题中遗漏了相关代码?在我用一个非常基本的布局进行测试时,我发布的内容很好,但也只是使用默认的布局参数就可以提供匹配/匹配维度。当你构建一个简单的应用程序时,它的父级(透明)内部有ConstraintLayout
并设置android:layout\u width=“match\u parent”
和android:layout\u height=“match\u parent”
这两个版本,在子版布局上加上一些背景色,你会看到它确实有效。一定是其他原因导致了问题。我已经编码了android:layout\u width=“50dp”
和android:layout\u height=“50dp
查看我是否能得到什么,但不幸的是,没有显示任何内容,所有坐标仍然是0
。我刚刚尝试了您的代码,即使使用匹配\u父项
,它也能工作。请显示活动\u播放器的完整代码
。顺便说一句,你可以调用LayoutInflater.from(context).inflate(R.layout.notifier,view,true)
并将其添加到ViewGroup
@lelloman-我的代码是一个巨大的项目,需要很多页面:)那么,你为什么要在notifier
中实例化处理程序?你真的要在onCreate
中添加通知程序吗?@lelloman-是的,我的通知程序是Toast
的替代品,我需要处理程序的处理程序
取消通知。我刚刚尝试了您的代码,即使使用match\u parent
,也能正常工作。请显示活动\u播放器的完整代码
。顺便说一句,您可以从(上下文)调用LayoutInflater.from。
Notifier(ViewGroup view) {
handler = new android.os.Handler();
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);
}
android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
LayoutInflater inflater = LayoutInflater.from(view.getRootView().getContext());
notifierView = inflater.inflate(R.layout.notifier, view, false);