英文:
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:
NoActionBar:
英文:
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):
<?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>
Default ActionBar :
NoActionBar :
答案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.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论