【发布时间】:2015-10-09 11:47:30
【问题描述】:
我试图在我的工具栏中显示必要的内容(即标题和后退按钮),但由于某种原因没有出现,但我的片段中的主要内容却出现了。有什么办法可以解决这个问题?
MapActivity.java
public class MapActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_map);
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(R.id.imageView);
imageView.setImage(ImageSource.resource(R.drawable.map));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
final Intent intent = NavUtils.getParentActivityIntent(this);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
NavUtils.navigateUpTo(this, intent);
return true;
}
return false;
}
}
FragmentMap.java
public class FragmentMap extends android.support.v4.app.Fragment {
public FragmentMap() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_map, container, false);
Toolbar toolbar = (Toolbar) v.findViewById(R.id.actionBar);
toolbar.setBackgroundColor(Color.parseColor("#212121"));
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.map) + "</font>"));
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
activity.getSupportActionBar().setDisplayShowHomeEnabled(false);
return v;
}
}
fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentmap" >
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/actionBar"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="16dp" >
</android.support.v7.widget.Toolbar>
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>
</resources>
【问题讨论】:
标签: java android xml android-fragments android-toolbar