android - fragment 未显示

标签 android android-layout android-fragments

我遇到了一个问题,我可以创建一个 fragment ,它的 View 似乎已创建,但没有显示出来。 fragment 本身已创建,内部的任何代码都可以正常运行,但它只是在某处不可见。后退按钮也可以很好地与其交互(它“关闭”它),它只是没有实际显示在屏幕上(仅显示主布局)。

来 self 的 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>

这是我的 fragment 类:

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” 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>

我对使用 fragment 很陌生,不太确定我的问题是在代码中还是与布局有关。我的目标是将“AddNamePane”添加到临时替换主视图的“根布局”。我所做的尝试是给我的根布局一个 id 并在 FragmentTransaction 中使用它。如果重要的话,我使用的 api 级别是 8(Android 2.2),因此使用 android.support.v4.app 包中的内容。

最佳答案

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

如果您使用实际的 xml 更新您的帖子,我可以向您展示具体的操作方法。

关于android - fragment 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884762/

相关文章:

java - 为什么我对字符串的空检查不起作用?

android - 如何在支持双卡双待的手机中选择主卡来发送短信?

android - CardView 内部的 TabLayout

Android 简单的 textview 文本更改与计时器

android - ViewFlipper 并在创建时显示不是第一项

android - 在 ViewPager 中使用 MapFragment 的最佳方式是什么?

android - FragmentPagerAdapter 滑动显示 ListView 1/3 屏幕宽度

android - RelativeLayout 中子级的 match_parent 属性

android - 从 fragment 中的 sqlite 动态设置复选框

android - 使用 Fragments 更改 ActionBar 标题