Android FragmentActivity 不显示操作栏

标签 android android-fragments

我正在尝试创建一个使用 fragment 的应用程序,但是当我尝试在 FragmentActivity 中显示一个 Fragment 时,操作栏没有出现。

fragment Activity 类:

public class NoteDetailsActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_details_fragment);


        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            NoteDetailsFragment details = new NoteDetailsFragment();
            details.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.note_details, details).commit();
        }
    }
}

fragment 类:

public class NoteDetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static NoteDetailsFragment newInstance(int index) {
        NoteDetailsFragment f = new NoteDetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        int index = getArguments().getInt("index");
        String message = "";
        switch (index) {
            case 0:
                message = "Start";
                break;
            case 1:
                message = "Second";
                break;
            case 2:
                message = "last";
                break;

        }

        TextView text = new TextView(getActivity());
        text.setText(message);
        return text;
    }
}

我试图在我的 FragmentActivity 类中实现 onCreateOptionsMenu 但没有任何反应。我还尝试在 Fragment 类中 setHasOptionsMenu(true) ,但这也没有任何作用。

如果感兴趣,这是我插入Fragment (note_details_fragment) 的布局:

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

    <FrameLayout
        android:layout_above="@id/save_note"
        android:id="@+id/note_details"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground" />

    <Button
        android:id="@+id/save_note"
        android:layout_alignParentBottom="true"
        android:text="@string/save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

编辑: 来自 list :

    <activity android:name=".activities.NoteDetailsActivity">
    </activity>

最佳答案

派生自类 ActionBarActivity 而不是 FragmentAcivity 应该可以解决您的问题:

public class NoteDetailsActivity extends ActionBarActivity {
   // ...
}

关于Android FragmentActivity 不显示操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072348/

相关文章:

java - 如何删除 TextView 中两个相同字符串值之一?

android - 如何创建 Facebook key 哈希?

java - 检测要显示的图像大小并通过延迟加载图像使用它

android - Fragment getView() 总是为 FragmentStatePagerAdapter 创建的 Fragment 返回 null

android - 改编自 Firestore 的 Recyclerview 未出现在 Fragment 上(带标签)

android - 创建虚拟设备出现错误 "The skin directory does not point to a valid skin"

java - 狗品种 API Android Studio

android - 无法从 FrameLayout 中删除 fragment ?

android - 自定义 SupportActionBar 在重新显示后看起来不同

android - 回到 FragmentActivity 后出现 IllegalStateException