【问题标题】:CollapsingToolbarLayout title disappearsCollapsingToolbarLayout 标题消失
【发布时间】:2015-10-02 04:29:13
【问题描述】:

我有一个 CollapsingToolBarLayout。当导航抽屉关闭时,工具栏上的标题会消失。只有当工具栏处于折叠状态时才会发生这种情况。虽然处于展开状态,但标题仍然存在。我检查了代码但我找不到错误。 这是我的主要活动代码:-

  import android.support.design.widget.CollapsingToolbarLayout;
    import android.support.design.widget.CoordinatorLayout;
    import android.support.v4.view.ViewPager;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBar;  
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import com.defcomm.invento.NavigationDrawerActivity;
     import android.support.v4.widget.NestedScrollView;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;

     public class INVENTO extends AppCompatActivity {

    private Toolbar toolbar;
    private CoordinatorLayout mCoordinator;
    private CollapsingToolbarLayout mCollapsableLayout;
    private NestedScrollView nestedScrollView;
    private ViewPager mPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        mCoordinator= (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mCollapsableLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        toolbar= (Toolbar) findViewById(R.id.appbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        mCollapsableLayout.setTitle(getResources().getString(R.string.app_name));
        nestedScrollView= (NestedScrollView) findViewById(R.id.rvToDoList);
        mCollapsableLayout.setExpandedTitleColor(getResources().getColor(R.color.textColor));
        mCollapsableLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.textColor));
        NavigationDrawerActivity drawerFragment= (NavigationDrawerActivity) getSupportFragmentManager().
                findFragmentById(R.id.navigation_drawer);
            drawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawerlayout), toolbar);


    }
    @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_invento, 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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
      }

主要活动xml文件:-

    <android.support.v4.widget.DrawerLayout

    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:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.defcomm.invento.INVENTO">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:fitsSystemWindows="true"
                android:background="#3f51b5"
                app:contentScrim="@color/primaryColor"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                    android:id="@+id/imageview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/index"
                    app:layout_collapseMode="parallax" />
                <android.support.v7.widget.Toolbar

                    android:id="@+id/appbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"

                    app:layout_scrollFlags="scroll|enterAlways"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:layout_collapseMode="pin"/>


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

        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/rvToDoList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                android:textSize="26sp"
                android:background="@android:color/white"
                android:padding="5dp"

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

        <fragment
            android:id="@+id/navigation_drawer"
            android:name="com.defcomm.invento.NavigationDrawerActivity"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.design.widget.CoordinatorLayout>
    </android.support.v4.widget.DrawerLayout>
     }

我的导航java文件:-

 import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.support.annotation.Nullable;  
    import android.support.design.widget.CoordinatorLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.view.LayoutInflater;
    import android.support.design.widget.CollapsingToolbarLayout;
    import android.view.View;
    import android.view.ViewGroup;

     public class NavigationDrawerActivity extends Fragment {


    private ActionBarDrawerToggle mdrawerToggle;
    private DrawerLayout mdrawerLayout;
    private boolean mUserLearnedState;
    private CoordinatorLayout mcoordinator;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    View containerId;
    public static final String file_pref_name = "Testpef";
    public static final String KEY_USER_VALUE = "user_learned_drawer";
    private boolean mfromSavedInstanceState;

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

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserLearnedState = Boolean.valueOf(readPreference(getActivity(), KEY_USER_VALUE, "false"));
        if (savedInstanceState != null) {
            mfromSavedInstanceState = true;
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout=inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        //recyclerview= (RecyclerView) layout.findViewById(R.id.recycler);
        return layout;

    }


    public void setUp(final int fragmentId, DrawerLayout drawerlayout, final Toolbar toolbar) {
        mdrawerLayout = drawerlayout;

        containerId = getActivity().findViewById(fragmentId);
        mdrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerlayout, toolbar,
                R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!mUserLearnedState) {
                    mUserLearnedState = true;
                    saveToPreference(getActivity(), KEY_USER_VALUE, mUserLearnedState + "");
                }
                getActivity().invalidateOptionsMenu();
                toolbar.setTitle(getResources().getString(R.string.app_name));
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
                toolbar.setTitle(getResources().getString(R.string.app_name));
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                if (slideOffset < 0.5f) {
                    toolbar.setAlpha(1 - slideOffset);
                }
            }
        };
        if (!mUserLearnedState && !mfromSavedInstanceState) {
            mdrawerLayout.openDrawer(containerId);
        }

        mdrawerLayout.setDrawerListener(mdrawerToggle);
        mdrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mdrawerToggle.syncState();

            }
        });

    }


    public static void saveToPreference(Context context, String preferenceName, String preferenceValue) {
        SharedPreferences shared = context.getSharedPreferences(file_pref_name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shared.edit();
        editor.putString(preferenceName, preferenceValue);
        editor.apply();
    }

    public static String readPreference(Context context, String preferenceName, String defaultValue) {
        SharedPreferences share = context.getSharedPreferences(file_pref_name, Context.MODE_PRIVATE);
        return share.getString(preferenceName, defaultValue);
    }

}

【问题讨论】:

    标签: android navigation-drawer android-collapsingtoolbarlayout android-titlebar


    【解决方案1】:

    我遇到了类似的问题,发现有一个示例 repo 用于演示此问题 here。如果该链接不起作用,请查看我的分叉版本here

    经过一些随机实验,我发现删除 AppBarLayout 上设置的android:fitsSystemWindows="true"(属性)可以解决这个问题。附上Fixed layout fileMainActivity供大家参考。

    仅供参考,您在 NavigationDrawerActivity 上定义的 setup 方法尝试在工具栏上设置标题,但根据官方指南,您需要在 CollapsingToolBarLayout 而不是工具栏上设置标题。 我建议您使用NavigationView(来自设计支持库)而不是自定义 DrawerFragment,除非您想向导航抽屉添加花哨/自定义行为。

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2015-10-11
      • 1970-01-01
      相关资源
      最近更新 更多