如何在android kotlin工具栏上添加后退按钮

Jac*_*ack 0 android toolbar kotlin android-actionbar

我现在在android上,我试图使用kotlin在工具栏上添加后退按钮,几乎尝试了网上发布的所有方法,它根本不起作用,总是因未知原因崩溃。有人可以帮忙吗?

class MainActivity : BaseActivity() {


  override fun getLayoutId(): Int {
      return  R.layout.activity_main
  }

  override fun initData() {
    super.initData()

    val testButton = findViewById<Button>(R.id.testButton)

    testButton.setOnClickListener{
        startActivity<RegistrationActivity>()
    }

  }

}

<?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"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">


<include android:id="@+id/navBarView" layout="@layout/toolbar"/>

<Button
    android:id="@+id/testButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="92dp"
    android:layout_marginBottom="533dp"
    android:height="50dp"
    android:text="click me"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/navBarView" />


</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

// 下面是工具栏 xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="@color/white"
   app:titleTextColor="@color/white"
   app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" >

<TextView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:src="@mipmap/title_view_image"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Toolbar" />

</androidx.appcompat.widget.Toolbar>



class RegistrationActivity: BaseActivityWithBinding<ActivityRegistrationBinding>() {

   private lateinit var viewModel: UserViewModel

   override fun onCreated(savedInstanceState: Bundle?) {

      initToolbar()

      val factory = UserViewModelFactory()
      viewModel = ViewModelProviders.of(this,factory).get(UserViewModel::class.java)
      binding.myHandle = viewModel
      binding.lifecycleOwner = this

      viewModel.isStringEmpty.observe(this, Observer {
         if(it == true){
            showAlert()
         }
      })

}

fun initToolbar(){

    val toolbar = findViewById<Toolbar>(R.id.toolbar);
    setSupportActionBar(toolbar)
    supportActionBar?.apply {
        setDisplayShowHomeEnabled(false)
        setHomeAsUpIndicator(R.drawable.ic_baseline_keyboard_arrow_down_24)
    }

}
Run Code Online (Sandbox Code Playgroud)

后退按钮不显示,我在网上看到了不同的解决方案,我尝试了所有这些,但不起作用,任何人都可以检查我是否错过了某些内容或?

小智 5

工具栏 xml 代码

<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B0CAB2"
tools:context=".SecondActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:elevation="4dp"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second Activity"
    style="@style/TextAppearance.MaterialComponents.Headline3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

样式.xml

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

活动.kt

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_second)

    // set toolbar as support action bar
    setSupportActionBar(toolbar)

    supportActionBar?.apply {
        title = "Toolbar Back Button Example"

        // show back button on toolbar
        // on back button press, it will navigate to parent activity
        setDisplayHomeAsUpEnabled(true)
        setDisplayShowHomeEnabled(true)
    }
}
Run Code Online (Sandbox Code Playgroud)