片段未显示 [英] Fragment not showing up

查看:70
本文介绍了片段未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个可以创建片段的问题,它的视图似乎已创建,但是没有显示.该片段本身已创建,并且其中的任何代码都可以正常运行,但是在某处是不可见的.后退按钮也可以很好地与之交互(关闭"它),它实际上并没有显示在屏幕上(仅显示主布局).

I'm having an issue where I can create a Fragment, its view appears to be created, but it doesn't show up. The fragment itself is created and any code inside runs without issue, but it is just invisible somewhere. The back button also interacts with it just fine (it "closes" it), it just doesn't physically show up on screen (only the main layout is displayed).

来自我的FragmentActivity:

From my 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();
}

这是首次显示的视图的布局(activity_main_view.xml,也称为我的根布局"):

This is the layout for the View that is first displayed (activity_main_view.xml aka my 'root layout'):

<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"片段的布局:

This is the layout for that "AddNamePane" fragment:

<?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软件包中的内容.

I am pretty new to using fragments, not too sure if my issue is in code or if it is something to do with the layouts. My goal is to add the "AddNamePane" to the 'root layout' replacing the main view temporarily. What I did to attempt this was to give my root layout an id and use that in the FragmentTransaction. If it matters, the api level I am using is 8 (Android 2.2) thus using things from android.support.v4.app package.

推荐答案

您正在尝试以编程方式替换LinearLayout,同时还希望它具有硬编码的子代.这行不通.您应该为FragmentTransaction使用一个FrameLayout,它也是layout_root的子代.

You're trying to programmatically replace a LinearLayout, while also expecting it to have the hard-coded children. This won't work. You should use a FrameLayout for your FragmentTransaction that is also a child of your layout_root.

如果您使用实际的xml更新帖子,那么我可以确切地向您展示如何执行此操作.

If you update your post with the actual xml, I can show you exactly how to do this.

这篇关于片段未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆