我已經在一個類中擴展片段活動,我也需要一個自定義工具欄。所以,我添加了代碼來獲取工具欄,但是setSupportActionBar(工具欄)沒有工作。然後,我添加了AppCompatActivity.getActivity()來投射,但這並沒有起作用。不能解決方法getActivity()在一個類中擴展FragmentActivity
下面是代碼 -
public class main_fragment extends FragmentActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar toolbar;
private FragmentDrawer drawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fragment);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
我試過了,不行。它顯示無法解析'ActionBarCompat'的符號。 – TeeKay