我在使用Android应用时出现问题,主要是因为当我将一个片段加载到活动中时,它永远不会在应用中显示该图标(它显示3个点,好像那里没有空间用于要显示的图标,它会显示文本)。 我的活动是使用导航抽屉,我不知道它是否与问题有关,我已经阅读了几个具有相同问题的答案,但没有一个解决方案似乎会影响行为。 如果我以编程方式添加图标它显示正常,但每当我尝试使用XML时,它都不会将图标显示为操作。
我的目标是minSDK = 14和targetSDK = 19
这是我的主要活动
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>
答案 0 :(得分:2)
您正在使用原生操作栏,而不是appcompat-v7
后端,正如您从Activity
继承并调用getActionBar()
所证明的那样。因此,请从菜单资源中删除app
命名空间,并将app:showAsAction
更改为android:showAsAction
。