片段未显示出来

20

我遇到了一个问题,我可以创建一个Fragment,它的视图似乎已经创建好了,但是没有显示出来。Fragment本身已经创建了,其中的任何代码都可以正常运行,但是它只是在某个地方不可见。返回按钮也可以与其良好交互(“关闭”它),它只是不会在屏幕上物理显示出来(只显示主要布局)。

从我的FragmentActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_view);

    // some logic here that sets a button listener that calls openAddNamePane() when pressed
}

public void openAddNamePane(){
    AddNamePane addNamePane = new AddNamePane();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.layout_root, addNamePane, "AddnamePane");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

这是首次显示的 View 的布局(activity_main_view.xml,也称为我的“根布局”):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    android:orientation="vertical"

    android:background="@color/white"
    android:id="@+id/layout_root">

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button android:id="@+id/addPlayerButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Player" />

        <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/buttonLabel" />
    </LinearLayout>


    <ListView android:id="@+id/playerListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@color/white"
        android:entries="@array/testStringArray" >
    </ListView>

</LinearLayout>

这是我的片段类:

public class AddNamePane extends Fragment {
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("AddNamePane.onCreateView()");

        View view = inflater.inflate(R.layout.add_player_pane_layout, container, false);

        return view;
    }

    // a few other methods here, onAttach(), onStop(), onPause(), etc
    // all are empty of logic with just a println to see if they are being called correctly (in which they are)
}

这是“AddNamePane”片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="test name here"
        android:hint="Name here" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

我对使用片段还比较新,不太确定我的问题是代码问题还是布局问题。我的目标是将“AddNamePane”添加到“根布局”中,暂时替换主视图。我尝试的方法是给我的根布局一个id,并在FragmentTransaction中使用它。如果有影响的话,我使用的api级别是8(Android 2.2),因此使用来自android.support.v4.app包的东西。


你尝试过在根布局中创建一个“片段持有者”类型的FrameLayout或类似的控件来显示你的片段吗?然后在你的代码中,你可以引用这个新元素 - fragmentTransaction.replace(R.id.fragmentHolder, addNamePane, "AddnamePane"); - CodeMonkey
尝试直接“覆盖”我的主布局,我希望通过将我的主布局重用为其他片段的父/根来减少一个“层次结构”。但是保罗似乎在说这是不允许的。 - mitim
3个回答

20
你试图以编程方式替换一个 LinearLayout,同时期望它具有硬编码的子项。这种方法行不通。你应该使用一个 FrameLayout 作为你的 FragmentTransaction,并将其作为 layout_root 的子项。 如果更新您的帖子并提供实际的xml代码,我可以向您展示如何做到这一点。

嗨,保罗,我添加了其余的布局xml,尽管稍后它可能会因为我添加了一些东西而有所改变(但是当时我可以解决这个问题)。简而言之,似乎无法使用包含内容的“根视图”来附加另一个视图/片段?这导致硬编码视图始终优先,并且我应该有一个包含“nothing”的根,并将我的第一个/主要视图附加到其中,任何片段也附加到该根上?嗯,我想我以前没有使用过'FrameLayout'。 - mitim
等一下。第二天重新阅读您的答案后,看起来我搞错了:您建议我添加一种“容器”(FrameLayout元素)到我的主视图中,并将我的片段附加到其中。我确实尝试过类似的东西,但是我放进去的东西(我想是另一个LinearLayout)最终占据了整个屏幕并推出了所有原始根元素。我已将其设置为占据整个屏幕,因为我希望我的片段占据整个屏幕。我将查看FrameLayout。 - mitim
你可以在片段事务完成后设置帧布局的尺寸参数吗? - CodeMonkey
经过一些调整,我的片段现在出现了,但它出现在顶部并将主视图的内容向下推,而不是出现在顶部并占据整个屏幕。我真正想要的是片段只是出现在顶部并覆盖初始视图的内容。有什么办法可以实现这个效果吗? - mitim
我知道这很长时间,但这正是我的情况,我实际上花了两天的时间才解决问题!感谢您提供简短的答案。 :) - Harisewak
你救了我的命,太感谢了。我遇到了https://stackoverflow.com/q/37602606/3749773的问题。 - Alex

6
我曾遇到类似的问题,碎片没有显示出来。问题在于我删除了AndroidAnnotations,但忘记将代码从onViewCreated和onCreate方法移动到onCreateView中。通过比较两个碎片的代码,一个显示出来,另一个没有,我发现了这个错误。因此,如果您遇到类似的问题,请检查碎片的代码。它可能一开始不会显示任何错误,但由于这些错误而不能正常工作或显示出来。
特别是,请检查您是否有此方法,并且您是否填充了视图:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
    return v;

}

在我的情况下,我更新了这种方法,但忘记了返回被填充的视图,而是返回了null! - Abdelalim Hassouna

0
tools:layout="@layout/fragment_up"

请按照上述方式使用您的布局文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接