gpt4 book ai didi

Android 刷新按钮不会出现在操作栏上

转载 作者:行者123 更新时间:2023-11-29 20:03:34 26 4
gpt4 key购买 nike

我是 android 的新手,正在研究 fragment 、操作栏和菜单。我在菜单中放了一个刷新按钮,我正在从我的 fragment 中膨胀它,但它没有显示。这是我的代码。谁能告诉我我做错了什么?

菜单

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_reload"
android:title="@string/action_reload"
android:showAsAction="always" android:icon="@drawable/nav_btn_reload"/>
</menu>

fragment

public class NearbyFragment extends BaseFragment implements OnConnectionFailedListener, LocationListener, LoaderCallbacks<List<User>> {

LocationClient locationClient;
Location lastLocation;
private GoogleMap mMap;
private HashMap<Marker, User> mHashMap = new HashMap<Marker, User>();
private static View view;
public void onResume () {

super.onResume();
getActivity().invalidateOptionsMenu();


//setHasOptionsMenu(false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActivity().getActionBar().setCustomView(R.layout.actionbar_main);


if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(com.company.stush.R.layout.fragment_nearby, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}






@Override
public void onActivityCreated (Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

//setHasOptionsMenu(true);

LocationManager manager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

if(!statusOfGPS)
{
Toast.makeText(getActivity(), "Turn on 'Location Services' to acces this feature.",
Toast.LENGTH_LONG).show();
}


mMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(com.company.stush.R.id.map)).getMap();


mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

@Override
public void onInfoWindowClick(Marker marker) {

User user = mHashMap.get(marker);
Intent intentUser = new Intent(getActivity(), ProfileActivity.class);
intentUser.putExtra("userID",user.userID);
getActivity().startActivity(intentUser);
//Log.d("GODD", "Click");
} });




locationClient = new LocationClient(getActivity(), new GooglePlayServicesClient.ConnectionCallbacks() {




@Override


public void onDisconnected() {
Log.d("Eror1", "Disconect");

}

@Override
public void onConnected(Bundle connectionHint) {
LocationRequest locationRequest = LocationRequest.create();
locationClient.requestLocationUpdates(locationRequest, NearbyFragment.this);
}
}, this);
locationClient.connect();
mMap.setMyLocationEnabled(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

inflater.inflate(com.company.stush.R.menu.nearby, menu);
}

@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("Eror1", "Disabled GPS");

}

@Override
public void onLocationChanged(Location location) {
Log.d("Eror", "Disabled GPS");
if (lastLocation == null || location.distanceTo(lastLocation) > 1000) {
lastLocation = location;
getLoaderManager().initLoader(0, null, this);

}
}



public void onProviderDisabled(String provider) {
Log.d("Eror", "Disabled GPS");

}





@Override
public Loader<List<User>> onCreateLoader(int arg0, Bundle arg1) {
NearbyLoader loader = new NearbyLoader(getActivity(), this.lastLocation);
return loader;
}

@Override
public void onLoadFinished(Loader<List<User>> arg0, List<User> users) {
Log.d("USERS", users.toString());
for (User user : users) {
MarkerOptions options = new MarkerOptions();
//.title("You are here")
options.icon(BitmapDescriptorFactory.fromResource(com.company.stush.R.drawable.map_pin));
options.position(new LatLng(user.latitude, user.longitude));
options.title(user.userFullName);
options.snippet(user.companyAddress);

Marker m =mMap.addMarker(options);

mHashMap.put(m, user);
}
}

@Override
public void onLoaderReset(Loader<List<User>> arg0) {

}

我试了一天都没有成功,请新手帮忙解决。提前谢谢你。

最佳答案

您需要在Fragment中的onCreate中调用setHasOptionsMenu(true);来显示ActionBar

public class NearbyFragment extends BaseFragment implements OnConnectionFailedListener, LocationListener, LoaderCallbacks<List<User>> {
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
inflater.inflate(com.company.stush.R.menu.nearby, menu);
super.onCreateOptionsMenu(menu, inflater);
}
...
}

关于Android 刷新按钮不会出现在操作栏上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35934623/

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