首页 > 解决方案 > 如何制作顶栏?

问题描述

为什么当我编写此代码时应用程序崩溃?

    Toolbar mToolbar = findViewById(R.id.scroll_top_bar);
    setSupportActionBar(mToolbar);

这是xml代码

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

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher_background" />


            <androidx.appcompat.widget.Toolbar
                android:id="@+id/scroll_top_bar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
</LinearLayout>

这是java代码

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_informations);

        Toolbar mToolbar = findViewById(R.id.scroll_top_bar);
        setSupportActionBar(mToolbar);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_display_informations,menu);
        return true;
    }

“您可能会在我的问题中发现一个错误,因为我是 stack over flow 平台的新手”

标签: javaandroidandroid-studio

解决方案



推荐阅读