1

我有一个Fragment空构造函数,我需要能够访问字符串资源,以便我可以创建片段的构造组件。

我知道我可能应该使用newInstance()构造函数而不是构造函数,但构造函数到目前为止一直在工作,使用Bundle.

我的片段构造函数如下:

public SideMenuView(Context ctx) {

    menuItems = new MenuItem[] {

            new MenuItem(R.drawable.one_icon, ctx.getResources().getString(R.string.one), R.id.imgChevron),
            new MenuItem(R.drawable.two_icon, ctx.getResources().getString(R.string.two), R.id.imgChevron),
            new MenuItem(R.drawable.three_icon, ctx.getResources().getString(R.string.three), R.id.imgChevron)
        };

    fragCache = new Fragment[menuItems.length];
}
4

2 回答 2

1

为什么需要在构造函数中获取这些资源?我只是将那段代码移到onCreate()方法中。

于 2013-03-27T16:47:29.007 回答
0

在片段中使用菜单就足够了:

a) 添加以下从 XML 资源构建菜单的方法

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
     inflater.inflate(R.menu.attendance_fragment, menu);
}

b) 添加

setHasOptionsMenu(true);

在片段构造函数中提示 android 你的片段想要参与填充选项菜单

于 2013-03-27T16:50:07.997 回答