我为谷歌地图创建了一个片段活动,但操作栏和菜单没有显示我尝试了不同的解决方案,但没有一个主题解决了问题
public class FitnessTrackerActivity extends FragmentActivity {
private GoogleMap map;
GpsTracker gps;
TrackDBHelper mDBHelper;
// Fields for flash functionality
Camera camera;
Camera.Parameters parameters;
String date;
TextView transportation;
TextView distance;
TextView calories;
Chronometer chrono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fitness_tracker);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
date = today.format("%k:%M:%S");
if (isFlashSupported()) {
camera = Camera.open();
parameters = camera.getParameters();
} else {
showNoFlashAlert();
}
}
@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_fitness_tracker, menu);
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.
switch (item.getItemId()) {
case R.id.led_on:
// Turn on LED
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
return true;
case R.id.led_off:
// Turn off LED
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showNoFlashAlert() {
new AlertDialog.Builder(this)
.setMessage("Your device hardware does not support flashlight!")
.setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).show();
}
private boolean isFlashSupported() {
PackageManager pm = getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
}
这是我正在尝试使用的菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FitnessTrackerActivity">
<item android:id="@+id/led_on"
android:title="LED On"
app:showAsAction="withText"/>
<item android:id="@+id/led_off"
android:title="LED Off"
app:showAsAction="withText"/>
</menu>
这是FitnessTrackerActivity XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".FitnessTrackerActivity"
tools:showIn="@layout/activity_fitness_tracker">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_marginBottom="150dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnStartStop"
android:onClick="onButtonStartStopClick"
android:layout_alignParentBottom="true"
android:id="@+id/btnStartStop"
android:layout_marginBottom="40dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/caloriesVal"
android:text="0"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="40dp"
android:layout_marginRight="20dp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/caloriesText"
android:text="@string/caloriesText"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/caloriesVal"
android:layout_marginBottom="40dp"
android:layout_marginRight="5dp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timerText"
android:text="@string/timer"
android:layout_above="@id/caloriesText"
android:layout_toLeftOf="@+id/timerVal"
android:layout_marginRight="5dp"
android:textSize="20sp"/>
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timerVal"
android:layout_above="@id/caloriesText"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/distanceVal"
android:text="0"
android:layout_above="@id/timerText"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/distanceText"
android:text="@string/distanceText"
android:layout_above="@id/timerText"
android:layout_toLeftOf="@id/distanceVal"
android:layout_marginRight="5dp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/transportationSel"
android:layout_above="@id/distanceText"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/transportationText"
android:text="@string/transportationText"
android:layout_above="@id/distanceText"
android:layout_toLeftOf="@id/transportationSel"
android:layout_marginRight="5dp"
android:textSize="20sp"/>
</RelativeLayout>
答案 0 :(得分:0)
ActionBar使用AppCompatActivity,但你可以将googleMap放在片段中,使用setChildFragmentMenger并将layoutFragment放在AppCompatActivity布局中,将ActionBar放在AppCompatActivity中,希望对其有所帮助