当前位置: 首页 > 知识库问答 >
问题:

CollapsingToolbarLayout标题消失

冯驰
2023-03-14
  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);
      }
    <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);
    }

}

共有1个答案

聂季同
2023-03-14

我有类似的问题,并发现有一个例子repo是创建来演示这个问题在这里。如果这个链接不起作用,请在这里签出我的分叉版本

经过一些随机实验,我发现删除AppBarLayout上设置的Android:fitssystemWindows=“true”(attribute)可以解决这个问题。附加固定的布局文件和MainActivity供您参考。

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

 类似资料:
  • 下面是我的布局: 我希望标题停留在工具栏中,而不是在折叠ToolbarLayout中。因此我将代码从: 在调用setSupportActionBar(mToolbar)之后,标题和菜单都是不可见的。

  • 问题内容: 我设置了一个tableView,以便在触摸一个单元格时,它的高度会扩展以显示更多信息。tableView有5个部分。 我有一个错误:当单元格扩展时,该单元格下面的所有headersCells都不可见。控制台输出以下内容:“ [31233:564745]没有正在重用的表单元格的索引路径” 在我的情节提要中,我有2个自定义单元:用于数据承载单元和标题。 如果我在开始/结束更新之间输入,尽管

  • 本文向大家介绍Android5.0+ CollapsingToolbarLayout使用详解,包括了Android5.0+ CollapsingToolbarLayout使用详解的使用技巧和注意事项,需要的朋友参考一下 CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含

  • 我正在创建一个简单的tkinter窗口弹出窗口(粘贴链接:http://pastebin.com/kvv0G4rM).我面临的问题是标题信息(“输入费用”)从弹出的tkinter窗口中被切断。 我尝试过调整tkinter窗口的大小并将其移到中心。在这种情况下,标签、按钮的所有内容都不会完全填满屏幕。(尝试注释掉高度和尺寸注释以及.geometry函数调用) 看起来像这样 标签和按钮的当前场景 我使