我的片段中可以看到两个工具栏

时间:2015-08-03 06:58:57

标签: android fragment toolbar

我一直使用navigation drawer处理toolbar,点击drawer items时,会显示相应的fragments,但这是问题,我什么时候有点击drawer itemsfragments显示两个toolbars。请帮助。

fragment.xml之

    <FrameLayout 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"
    tools:context="archerpenny.impdrawerfragment.BlankFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
    </LinearLayout>

</FrameLayout>

My Activity_main.xml..

`<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Container"
        android:orientation="vertical">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="hi"/>



    </FrameLayout>
    </LinearLayout>




    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/DrawerList"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:background="@mipmap/menu_bg"
        android:layout_gravity="left"/>

</android.support.v4.widget.DrawerLayout>


MainActivity.java....

`



import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;


public class MainActivity extends AppCompatActivity  {
    ActionBarDrawerToggle mDrawerToggle;
    RecyclerView.Adapter mAdapter;
    RecyclerView recyclerView;
    DrawerLayout mDrawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdapter = new NavigationDrawerAdapter(this);
        mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        NavigationDrawerAdapter adapter;

        recyclerView = (RecyclerView)findViewById(R.id.DrawerList);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(mAdapter);

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.open,R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        // do whatever
                        if(position==0)
                        {
                            BlankFragment blankFragment=new BlankFragment();
                            FragmentManager fragmentManager = getFragmentManager();
                            fragmentManager.beginTransaction()
                                    .replace(R.id.Container, blankFragment)
                                    .commit();
                        }
                        mDrawerLayout.closeDrawers();
                    }
                })
        );
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}


BlankFragment.java




import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class BlankFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER



    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();

        return fragment;
    }

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        AppCompatActivity activity = (AppCompatActivity) getActivity();

        View view=inflater.inflate(R.layout.fragment_blank, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // Inflate the layout for this fragment
        return view;
    }


}

2 个答案:

答案 0 :(得分:1)

删除片段中的include语句

<FrameLayout 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"
tools:context="archerpenny.impdrawerfragment.BlankFragment">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />
</LinearLayout>

答案 1 :(得分:0)

对于那些从一个片段中搜索两个Toolbar的人。

有一个类似的主题:Double Toolbar Is Showing on Fragment

AndroidManifest中设置主题:

<activity 
    android:name=".YourActivity"
    android:label="@string/title"
    android:theme="@style/AppTheme.NoActionBar" />

主题是:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

在YourActivity中,将其从AppCompatActivity扩展并添加ActionBar,所以写:

class YourActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fragment)

        setToolbar(toolbar)

        showFragment()

        // Handle onResume() in fragments, if needed.
        supportFragmentManager.addOnBackStackChangedListener {
            supportFragmentManager.fragments.lastOrNull()?.onResume()
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // 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.
        return when (item.itemId) {
            android.R.id.home -> {
                onBackPressed()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    fun setToolbar(toolbar: Toolbar) {
        setSupportActionBar(toolbar)
        // Show back arrow.
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)

        // Additional settings, if needed.
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
        val toolbarTextColor = ContextCompat.getColor(this, R.color.blue)
        toolbar.setTitleTextColor(toolbarTextColor)
        toolbar.navigationIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
        toolbar.overflowIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
    }

    private fun showFragment() {
        if (supportFragmentManager.findFragmentByTag(YourFragment.TAG) == null) {
            val fragment = YourFragment.newInstance()
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, fragment, YourFragment.TAG)
                .commit()
        }
    }
}

这是YourActivity的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</android.support.design.widget.CoordinatorLayout>

先前的尝试(请勿重复)。

我做了很多实验。创建了一个新项目。复制了所有可疑活动和片段,stylescolorsstringsdimens,更改了AndroidManifest。设置活动的主题而非...NoActionBar样式。已在活动布局中删除带有周围Toolbar标签的<android.support.design.widget.AppBarLayout>。在onCreate()中写道:

//    setSupportActionBar(toolbar) // Crashing string.
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)

然后我将项目复制到另一个文件夹,编译后显示一个工具栏。即使在源文件夹中启动了File > Invalidate caches and Restart,也没有任何反应。因此,在使缓存无效之后,Build > Rebuild Project(可能是Build > Clean Project)显示了一个工具栏。我什至还回来了AppBarLayout。我指责Android Studio。

但是第二天,这种魔力停止了执行。我编译了同一项目,并再次得到两个Toolbar。复制文件夹,使/build文件夹无效,无济于事。因此,我开始研究存储库提交以获取解决方案。它写在答案的开头。