片段不显示图标

Fragment not showing icon

我在使用 android 应用程序时遇到问题,主要是因为当我将片段加载到 activity 时,它从不在应用程序中显示图标(它显示 3 个点,好像有没有 space 来显示图标,而是显示文本)。 我的 activity 正在使用导航抽屉我不知道它是否与问题有关,我已经阅读了几个关于相同问题的答案,但似乎没有一个解决方案会影响行为。 如果我以编程方式添加图标它显示正常,但每当我尝试使用 XML 它永远不会将图标显示为动作。

我的目标是 minSDK = 14 和 targetSDK = 19

这是我的主要内容activity

public class MainActivity extends Activity {
/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
List<DrawerItem> dataList;
CustomDrawerAdapter adapter;

/**
 * Used to store the last screen title. For use in {@link #()}.
 */
private CharSequence mTitle;
private CharSequence mDrawerTitle;

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

    dataList = new ArrayList<>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close){
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);


    mDrawerList = (ListView) findViewById(R.id.lvNavigationDrawer);
    //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    //mMenuTitles = getResources().getStringArray(R.array.menu_array);
    dataList.add(new DrawerItem("Perfil", R.drawable.ic_perfil));
    dataList.add(new DrawerItem("Code Redeemer", R.drawable.ic_coderedeemer));
    dataList.add(new DrawerItem("Mi ID", R.drawable.ic_id));
    dataList.add(new DrawerItem("Sucursales", R.drawable.ic_sucursales));
    dataList.add(new DrawerItem("GP Finder", R.drawable.ic_finder));
    dataList.add(new DrawerItem("Calculadora Intercambio", R.drawable.ic_intercambio));
    dataList.add(new DrawerItem("Notificaciones", R.drawable.ic_configuracion));
    dataList.add(new DrawerItem("Configuración", R.drawable.ic_configuracion));

    adapter = new CustomDrawerAdapter(this,R.layout.custom_drawer_item, dataList);
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    //mDrawerList.setAdapter(new ArrayAdapter<>(this,R.layout.drawer_list_item,mMenuTitles));


}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    Fragment content;
    // Create a new fragment and specify the planet to show based on position
    switch (position){
        case 0:
            content = new StubFragment();
            break;
        case 4:
            content = new FinderFragment();
            break;
        default:
            content = new FinderFragment();
    }
    setTitle(dataList.get(position).getItemName());
    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.flFragmentContainer, content)
            .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(dataList.get(position).getItemName());
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    return mDrawerToggle.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    //Pass any configuration change to the drawer
    mDrawerToggle.onConfigurationChanged(newConfig);
}

}

FinderFragment.java

public class FinderFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_finder, container, false);
    return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    /*menu.add(Menu.NONE,  /*//** group ID.. not really needed unless you're working with groups **//**//**//**//*
            0,         /*//** this is the items ID (get this in onOptionsItemSelected to determine what was clicked) **//**//**//**//*
            Menu.NONE, /*//** ORDER.. this is what you want to change **//**//**//**//*
            R.string.search_product) /*//** title **//**//**//**//*
            .setIcon(android.R.drawable.ic_menu_search)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);*/ //THIS WORKS CORRECTLY
    inflater.inflate(R.menu.menu_fragment_finder, menu);
    //super.onCreateOptionsMenu(menu,inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 0:
            return false;
        default:
            break;
    }
    return false;
}
}

menu_fragment_finder.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:title="@string/search_product"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always" />
</menu>

您正在使用本机操作栏,而不是 appcompat-v7 向后移植,您从 Activity 继承并调用 getActionBar() 就证明了这一点。因此,从菜单资源中删除 app 命名空间并将 app:showAsAction 更改为 android:showAsAction