gpt4 book ai didi

java - fragment 不显示图标

转载 作者:行者123 更新时间:2023-12-02 05:05:51 26 4
gpt4 key购买 nike

我在使用 Android 应用程序时遇到问题,主要是因为当我将 fragment 加载到 Activity 中时,它永远不会在应用程序中显示图标(它显示 3 个点,就好像没有空间可供图标显示一样,它会显示改为文本)。我的 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>

最佳答案

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

关于java - fragment 不显示图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27805771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com