Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从XML文件添加的视图未显示在其父视图中_Java_Android_Android Layout_Android Studio_Android View - Fatal编程技术网

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);