Android的FragmentActivity不显示操作栏 [英] Android FragmentActivity not showing the action bar

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

问题描述

我试图创建使用碎片的应用程序,但是当我试图表明一个片段 FragmentActivity 操作栏不会出现。

FragmentActivity类:

 公共类NoteDetailsActivity扩展FragmentActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.note_details_fragment);
        如果(getResources()。getConfiguration()。方向
                == Configuration.ORIENTATION_LANDSCAPE){
            //如果现在屏幕处于横向模式,就可以显示
            //对话框中,符合列表,这样,我们就不需要这一活动。
            完();
            返回;
        }        如果(savedInstanceState == NULL){
            //在初始设置,插上的细节片段。
            NoteDetailsFragment细节=新NoteDetailsFragment();
            details.setArguments(getIntent()getExtras());
            。getSupportFragmentManager()调用BeginTransaction()加(R.id.note_details,细节).commit()。
        }
    }
}

片段类:

 公共类NoteDetailsFragment扩展片段{
    / **
     *创建DetailsFragment的新实例,初始化为
     *在显示索引的文字。
     * /
    公共静态的newInstance NoteDetailsFragment(INT指数){
        NoteDetailsFragment F =新NoteDetailsFragment();        //供应索引输入作为参数。
        捆绑ARGS =新包();
        args.putInt(指数,指数);
        f.setArguments(参数);        返回F;
    }    公众诠释getShownIndex(){
        返回getArguments()调用getInt(索引,0);
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        如果(集装箱== NULL){
            //我们有不同的布局,并在他们这一个
            //片段的含框架不存在。片段
            //仍可以从保存的状态创建的,但有
            //没有理由去尝试创建视图的层次结构,因为它
            //将不被显示。请注意,这是不需要的 - 我们可以
            //只运行下面的code,其中我们会创建并返回
            //视图层次结构;它只是永远不会被使用。
            返回null;
        }        INT指数= getArguments()调用getInt(指数)。
        字符串消息=;
        开关(指数){
            情况下0:
                消息=开始;
                打破;
            情况1:
                消息=秒;
                打破;
            案例2:
                消息=最后;
                打破;        }        TextView的文本=新的TextView(getActivity());
        text.setText(消息);
        返回文本;
    }
}

我曾尝试实施 onCreateOptionsMenu 在我的 FragmentActivity 类,但没有任何反应。我也试过 setHasOptionsMenu(真)片段中的类,但是,这并不无论做任何事情。

如果它的利益,这里是他们的布局,我插入我的片段(note_details_fragment):

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_margin =@扪/ activity_vertical_margin
    机器人:layout_height =match_parent>    <的FrameLayout
        机器人:layout_above =@ ID / save_note
        机器人:ID =@ + ID / note_details
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =match_parent
        机器人:背景=:/>中的Andr​​oid ATTR / detailsElementBackground?    <按钮
        机器人:ID =@ + ID / save_note
        机器人:layout_alignParentBottom =真
        机器人:文字=@字符串/保存
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>< / RelativeLayout的>

编辑:
从清单:

 <活动机器人:名字=。activities.NoteDetailsActivity>
    < /活性GT;


解决方案

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

 公共类NoteDetailsActivity扩展ActionBarActivity {
   // ...
}

I'm trying to create an application that uses fragments, but when I try to show a Fragment in a FragmentActivity the action bar doesn't appear.

FragmentActivity class:

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 class:

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;
    }
}

I have tried to implement the onCreateOptionsMenu in my FragmentActivity class but nothing happens. I've also tried to setHasOptionsMenu(true) in the Fragment class, but that doesn't do anything either.

If it's of interest, here's they layout where I insert my 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>

EDIT: From manifest:

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

解决方案

Deriving from class ActionBarActivity instead of FragmentAcivity should solve your problem:

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

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

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