简体   繁体   English

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

[英]how to add back button on toolbar android kotlin

I'm now to android, I am trying to add back button on toolbar using kotlin, tried almost every way posted on online, it just doesn't work at all, always crash for unknown reasons.我现在是 android,我正在尝试使用 kotlin 在工具栏上添加后退按钮,几乎尝试了在线发布的所有方法,它根本不起作用,总是因未知原因崩溃。 Can anyone help?谁能帮忙?

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>

// below is the toolbar xml //下面是工具栏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)
    }

}

the back button doesn't show, I see different solutions online, and I tried them all, it doesn't work, can anyone check if I miss something or?后退按钮不显示,我在网上看到了不同的解决方案,我尝试了所有的解决方案,但没有用,任何人都可以检查我是否遗漏了什么或?

Toolbar xml code工具栏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>

style.xml风格.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>

Activity.kt活动.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)
    }
}
setSupportActionBar(findViewById(R.id.toolbar))
// Get a support ActionBar corresponding to this toolbar and enable the Up button
supportActionBar?.setDisplayHomeAsUpEnabled(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM