无法在 Kotlin 中将 CustomToolbar 替换为 ActionBar。

huangapple go评论73阅读模式
英文:

Unable to replace CustomToolbar with ActionBar in Kotlin

问题

我创建了一个自定义的Toolbar并将其放置在活动中,但我遇到了这个错误:

java.lang.IllegalStateException: Activity com.example.citiesdistance.feature.main.MainActivity@3a04806 does not have an ActionBar set via setSupportActionBar()

注意:

当我将默认的ActionBar设置为显示时,自定义的Toolbar能够正常工作,但它位于默认的ActionBar下方。

CustomToolbar类:

class BaseToolbar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private lateinit var binding: ViewToolbarBinding

    init {
        if (attrs != null) {
            binding = ViewToolbarBinding.inflate(LayoutInflater.from(context), this, true)
            val attr = context.obtainStyledAttributes(attrs, R.styleable.BaseToolbar)
            val title = attr.getString(R.styleable.BaseToolbar_bs_title)
            if (!title.isNullOrEmpty())
                binding.toolbarTitleTv.text = title
            attr.recycle()
        }
    }
}

view_toolbar.xml(CustomToolbar Xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <ImageView
        android:id="@+id/backBtn"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:scaleType="centerInside"
        android:background="?selectableItemBackgroundBorderless"
        android:layout_gravity="center_vertical|start"
        android:layout_marginStart="16dp"
        app:srcCompat="@drawable/ic_back"/>
    <TextView
        android:id="@+id/toolbarTitleTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:layout_marginStart="56dp"
        style="?textAppearanceHeadline6"
        tools:text="نام صفحه"/>
</FrameLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    style="@style/WidthHeight.All.BothMatchParent"
    tools:context=".feature.main.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.example.view.BaseToolbar
            android:id="@+id/testToolBar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:bs_title="Page 1" />
    </com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.CitiesDistance" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        .
        .
        .
    </style>
</resources>

默认ActionBar:

无法在 Kotlin 中将 CustomToolbar 替换为 ActionBar。

NoActionBar:

无法在 Kotlin 中将 CustomToolbar 替换为 ActionBar。

英文:

I Created a Custom Toolbar and placed it in activity, but I got this error :

java.lang.IllegalStateException: Activity com.example.citiesdistance.feature.main.MainActivity@3a04806 does not have an ActionBar set via setSupportActionBar()

Note :

When I set the default ActionBar to show The Custom Toolbar work correctly, but it is under the default ActionBar.

CustomToolbar Class :

class BaseToolbar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private lateinit var binding: ViewToolbarBinding

    init {
        if (attrs != null) {
            binding = ViewToolbarBinding.inflate(LayoutInflater.from(context), this, true)
            val attr = context.obtainStyledAttributes(attrs, R.styleable.BaseToolbar)
            val title = attr.getString(R.styleable.BaseToolbar_bs_title)
            if (!title.isNullOrEmpty())
                binding.toolbarTitleTv.text = title
            attr.recycle()
        }
    }
}

view_toolbar.xml (CustomToolbar Xml):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;56dp&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
    &lt;ImageView
        android:id=&quot;@+id/backBtn&quot;
        android:layout_width=&quot;32dp&quot;
        android:layout_height=&quot;32dp&quot;
        android:scaleType=&quot;centerInside&quot;
        android:background=&quot;?selectableItemBackgroundBorderless&quot;
        android:layout_gravity=&quot;center_vertical|start&quot;
        android:layout_marginStart=&quot;16dp&quot;
        app:srcCompat=&quot;@drawable/ic_back&quot;/&gt;
    &lt;TextView
        android:id=&quot;@+id/toolbarTitleTv&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center_vertical|start&quot;
        android:layout_marginStart=&quot;56dp&quot;
        style=&quot;?textAppearanceHeadline6&quot;
        tools:text=&quot;نام صفحه&quot;/&gt;
&lt;/FrameLayout&gt;

activity_main.xml :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    style=&quot;@style/WidthHeight.All.BothMatchParent&quot;
    tools:context=&quot;.feature.main.MainActivity&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

        &lt;com.example.view.BaseToolbar
            android:id=&quot;@+id/testToolBar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;?actionBarSize&quot;
            app:bs_title=&quot;Page 1&quot; /&gt;
    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

themes.xml :

&lt;resources xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
    &lt;!-- Base application theme. --&gt;
    &lt;style name=&quot;Theme.CitiesDistance&quot; parent=&quot;Theme.MaterialComponents.DayNight.NoActionBar&quot;&gt;
        .
        .
        .
    &lt;/style&gt;
&lt;/resources&gt;

Default ActionBar :

无法在 Kotlin 中将 CustomToolbar 替换为 ActionBar。

NoActionBar :

无法在 Kotlin 中将 CustomToolbar 替换为 ActionBar。

答案1

得分: 0

在活动的onCreate()方法中,调用活动的setSupportActionBar()方法,并传递活动的工具栏。

英文:

I think the only step you are missing is the step 5 from https://developer.android.com/develop/ui/views/components/appbar/setting-up

> In the activity's onCreate() method, call the activity's setSupportActionBar() method, and pass the activity's toolbar.

答案2

得分: 0

我将默认的ActionBar设置为DarkActionBar,然后在Activity的onCreate方法中添加了supportActionBar?.hide()。如果我找到更好的解决方案,我会更新答案。

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- 应用程序的基础主题。 -->
    <style name="Theme.CitiesDistance" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        .
        .
        .
    </style>
</resources>
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    supportActionBar?.hide()
}
英文:

I set default ActionBar with DarkActionBar then add supportActionBar?.hide() in Activity's onCreate method.
I will update answer If I find a better sulotion.

&lt;resources xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
    &lt;!-- Base application theme. --&gt;
    &lt;style name=&quot;Theme.CitiesDistance&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;
        .
        .
        .
    &lt;/style&gt;
&lt;/resources&gt;


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    supportActionBar?.hide()
}

huangapple
  • 本文由 发表于 2023年5月29日 14:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定