Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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
Android LinearLayout中的片段在没有布局重量属性的情况下不显示_Android_Android Layout_Android Fragments_Android Linearlayout - Fatal编程技术网

Android LinearLayout中的片段在没有布局重量属性的情况下不显示

Android LinearLayout中的片段在没有布局重量属性的情况下不显示,android,android-layout,android-fragments,android-linearlayout,Android,Android Layout,Android Fragments,Android Linearlayout,我在一个活动(MyActivity)中有两个片段(MyListFragment,MyDetailFragment)。第一个片段是项目列表,第二个片段是项目的细节(这是另一个列表,但我认为这并不重要)。第一个列表片段在MyActivity启动时显示 MyListFragment视图: 问题是: 当我单击MyListFragment中的任何项目时,MyDetailFragment都不会显示(尽管我知道它的生命周期方法被正确调用) 我发现,当我在MyListFragment中将android:layo

我在一个活动(MyActivity)中有两个片段(MyListFragment,MyDetailFragment)。第一个片段是项目列表,第二个片段是项目的细节(这是另一个列表,但我认为这并不重要)。第一个列表片段在MyActivity启动时显示

MyListFragment视图:

问题是: 当我单击MyListFragment中的任何项目时,MyDetailFragment都不会显示(尽管我知道它的生命周期方法被正确调用)

我发现,当我在MyListFragment中将android:layout_weight=“1”放入片段定义时,它是有效的有人知道为什么吗?

据我所知,android:layout\u-weight属性只有在linearlayout中有更多视图时才应该发挥作用,但我只有一个

这是工作布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent" >
    <fragment class="myapp.ActionsList"
              android:id="@+id/actions_list_fragment"
              **android:layout_weight="1"**
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
</LinearLayout>

谢谢


您正在完全替换一个片段,因此在
fragment\u容器中一次只有一个片段的视图。最好将
fragment\u容器
a
FrameLayout
,并将fragment布局根的高度和宽度设置为
match\u parent

您使用的布局权重错误,我认为您不理解它的功能。为了在LinearLayout parrent中运行,必须有一个权重和,它将是布局的总权重。第二件事是根据线性布局方向设置孩子的体重或身高0dp。布局的每个子级都必须有一个布局权重,权重的总和必须等于weightSum。这样布局将根据您的体重自动拆分

好吧,我做错了一件事

只能替换以编程方式加载的片段。不是xml中定义的片段


但我不知道。因为MyListFragmentView在xml定义中包含片段,所以尽管我调用了replaced,该方法并没有用新片段替换actions\u list\u片段,而是将新片段放在actions\u list\u片段之后。这就是为什么布局权重能起作用-根线性布局中有两个子项,但我认为只有一个。

好吧,我在所有根布局中使用了匹配父项(分别填充父项)。当我将LinearLayout更改为FrameLayout时,它会以某种方式工作(新片段与第一个片段重叠)。但我还是错过了一些东西。为什么调用“replace”方法时会看到第一个片段。如果只有一个片段的视图,为什么必须设置布局权重?@fandasson当其中一个片段嵌入布局xml时,就会出现这个问题。如果您
add()
在运行时(在
onCreate()
中)添加代码的第一个片段,那么
replace()
将正常工作。您是对的。我已经把它贴在我的答案里了。不过还是要谢谢你。我知道重量的工作原理。我不明白的是,为什么将layout_weights属性设置为可以工作,尽管它不应该工作(因为我希望在MyListFragment中根LinearLayout中只有一个孩子,但我错了,请检查我对这个问题的回答)我愿意,但StackOverflow说我可以在两天内接受自己的答案。我在等它。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">

    <ListView
            android:id="@android:id/list"
            android:tag="@string/activities_list_tag"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

</LinearLayout>
@Override
public void onActionSelected(MAction action, Integer position){
/**some code...*/
    getFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, 
                     new MyDetailFragment(mAction,position)).commit();
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent" >
    <fragment class="myapp.ActionsList"
              android:id="@+id/actions_list_fragment"
              **android:layout_weight="1"**
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
</LinearLayout>
getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, 
                 new MyDetailFragment(mAction,position)).commit();