Android 与布局结合使用的带溢出的操作栏

Android 与布局结合使用的带溢出的操作栏,android,android-actionbar,Android,Android Actionbar,您好,我是android的初学者,我对带有动作溢出和片段的动作栏有疑问。 我试图制作的应用程序由三个独立的布局组成,我很难在这些布局的顶部显示一个操作栏 我设法使一个应用程序有3个布局,也使其他应用程序有行动溢出的行动栏,但我不知道如何将这两个结合成一个应用程序。 我一直在寻找答案,但我还没有找到能解决我问题的答案。 这是我的第一个问题,所以我希望我已经设法描述了我需要解决的问题。 感谢所有帮助我解决问题的人 这是我在AppOne中的代码(3个布局碎片) 片段布局xml文件: <?xml

您好,我是android的初学者,我对带有动作溢出和片段的动作栏有疑问。 我试图制作的应用程序由三个独立的布局组成,我很难在这些布局的顶部显示一个操作栏

我设法使一个应用程序有3个布局,也使其他应用程序有行动溢出的行动栏,但我不知道如何将这两个结合成一个应用程序。 我一直在寻找答案,但我还没有找到能解决我问题的答案。 这是我的第一个问题,所以我希望我已经设法描述了我需要解决的问题。 感谢所有帮助我解决问题的人

这是我在AppOne中的代码(3个布局碎片)

片段布局xml文件:

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="second layout - layout B"
    android:id="@+id/textView2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_main.xml代码:

<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
xmlns:android="http://schemas.android.com/apk/res/android">
这是我在action bar中使用的带有action overflow的示例:

第1部分:
第2部分:

在Android中,默认情况下可以获得ActionBar覆盖,您只需将活动类继承(扩展)到ActionBarActivity类。您还应该重写OnCreateOptions菜单方法。

我能知道您为什么使用两个不同的应用程序吗?我不明白你说的应用程序是什么意思。它们是指两个不同的项目/应用程序吗?对不起,我写了应用程序我应该写一个项目。我使用两个项目的唯一原因是,在第一个项目中,我使用三个屏幕(三个片段或三个布局)来练习项目,在第二个项目中,我的想法是创建一个带有动作溢出的动作栏。但是现在我正在尝试创建一个新项目,在这个项目中,我将把这两个选项合并在一起。默认情况下您将获得的操作溢出写入?在活动中,将其扩展到ActionBarActivity,这就足够了,对吗?是的,你是对的,解决了我的问题,非常感谢!我将添加我的评论作为答案,请接受它作为答案。
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
xmlns:android="http://schemas.android.com/apk/res/android">
package com.example.mp.app;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;

import java.lang.reflect.Field;


public class MainActivity extends FragmentActivity {

ViewPager viewPager = null;

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

    viewPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new MyAdapter(fragmentManager));

    makeActionOverflowMenuShown();
}

private void makeActionOverflowMenuShown() {
    //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        Log.d("TAG", e.getLocalizedMessage());
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    menu.clear();
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

class MyAdapter extends FragmentPagerAdapter {

MyAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    Log.d("MainActivity", "get Item is called                " + i);
    Fragment fragment = null;
    if (i == 0){
        fragment = new FragmentA();
        Log.d("MainActivity", "get Item is called             A             " + i);
    }
    if (i == 1){
        fragment = new FragmentB();
        Log.d("MainActivity", "get Item is called             B             " + i);
    }
    if (i == 2){
        fragment = new FragmentC();
        Log.d("MainActivity", "get Item is called             C             " + i);
    }

    return fragment;
}

@Override
public int getCount() {
    Log.d("MainActivity", "get Count is called");
    return 3;
}
}