Все подобные вопросы относятся к более старым версиям. Но у меня не было выбора, кроме как задать это. Мой импорт:
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
Класс MainActivity расширяет AppCompactActivity, идентификатор панели инструментов тот же, а панель инструментов инициализирована. Также для панели действий в стиле уже установлено значение NoActionBar. Реализация следующая:
toolbar= findViewById(R.id.toolbar1);
getSupportActionBar(toolbar);
Ошибка: getSupportActionBar () в AppCompactActivity нельзя применить к (androidx.appcompat.app.AppCompatActivity)
панели инструментов, определенной в activity_main. xml как
<include layout="@layout/toolbar"
android:id="@+id/toolbar1"/>
Код для файла ресурсов макета панели инструментов:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/HomeBtn"
android:background="@mipmap/home"
android:contentDescription="@string/homebtn" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random text"
android:textAlignment="center"
android:padding="5dp"/>
</androidx.appcompat.widget.Toolbar>
Как я могу заставить мою панель инструментов работать?