尽管出现片段内容,工具栏内容仍未显示

时间:2015-07-20 11:15:39

标签: java android xml android-fragments android-toolbar

我正在尝试在我的工具栏中显示必要的内容(即标题和后退按钮)但由于某种原因未出现,但我的片段中的主要内容确实如此。如何解决此问题?

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>

enter image description here

2 个答案:

答案 0 :(得分:2)

完整代码

MapActivity

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


public class MapActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        Toolbar toolbar = (Toolbar) findViewById(R.id.actionBar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setBackgroundColor(Color.parseColor("#212121"));
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(false);
        }

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MapActivityFragment())
                .commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_map, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                super. onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_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:background="@color/whiteColor"
    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>

    <LinearLayout
        android:id="@+id/container"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

styles.xml

<style name="AppThemeNew" parent="Theme.AppCompat.NoActionBar">
    </style>

MapActivityFragment.java

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A placeholder fragment containing a simple view.
 */
public class MapActivityFragment extends Fragment {

    public MapActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_map, container, false);
        getActivity().setTitle(Html.fromHtml("<font color='#FFFFFF'>" + "Hello Toolbar" + "</font>"));
        return rootView;

    }
}

fragment_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/whiteColor"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/blackColor"/>

</RelativeLayout>

答案 1 :(得分:0)

添加此

@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"));

    //---- changes ---
    ((MapActivity) getActivity()).setTitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.map) + "</font>"));
    ((MapActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((MapActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);


    return v;
}