如何为material components顶部应用程序栏中的导航图标设置onclick事件(android)

vktxenjb  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(436)

我使用材质组件(android)创建了一个顶部应用程序栏,并将导航图标从菜单图标更改为箭头图标。单击事件返回到上一页。我已经通读了官方文件,但在我的情况下不起作用。我不知道如何设置箭头图标的点击事件。感谢您的帮助。
一个更好的图像来理解我在说什么点击我
文档链接单击我
设置活动.java

  1. package studio.itztaylorau.dashboard;
  2. import android.os.Bundle;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.preference.PreferenceFragmentCompat;
  5. import studio.itztaylorau.dashboard.R;
  6. import studio.itztaylorau.dashboard.AccountActivity;
  7. public class SettingsActivity extends AppCompatActivity{
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.settings_activity);
  12. if (savedInstanceState == null) {
  13. getSupportFragmentManager()
  14. .beginTransaction()
  15. .replace(R.id.settings, new SettingsFragment())
  16. .commit();
  17. }
  18. View topAppBar = findViewById(R.id.topAppBar);
  19. topAppBar.setNavigationOnClickListener {
  20. Intent i = new Intent(getApplicationContext(), Account.class);
  21. startActivity(i);
  22. }
  23. }
  24. public static class SettingsFragment extends PreferenceFragmentCompat {
  25. @Override
  26. public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
  27. setPreferencesFromResource(R.xml.root_preferences, rootKey);
  28. }
  29. }
  30. }

设置活动.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. xmlns:app="http://schemas.android.com/apk/res-auto">
  5. <FrameLayout
  6. android:id="@+id/settings"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" >
  9. <com.google.android.material.appbar.AppBarLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <com.google.android.material.appbar.MaterialToolbar
  13. android:id="@+id/topAppBar"
  14. android:layout_width="match_parent"
  15. android:layout_height="?attr/actionBarSize"
  16. app:title="@string/topAppBarTitle_Settings"
  17. android:background="@color/appTopBar"
  18. app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
  19. />
  20. </com.google.android.material.appbar.AppBarLayout>
  21. </FrameLayout>
  22. </LinearLayout>
ewm0tg9j

ewm0tg9j1#

你在网上做一个碎片交易
settings FrameLayout 占位符,但此占位符还包含 AppBarLayout 工具栏的。
要解决此问题:
第一步:获取 AppBarLayout 从碎片中取出 FrameLayout 占位符,因此,将布局更改为:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <com.google.android.material.appbar.AppBarLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content">
  9. <com.google.android.material.appbar.MaterialToolbar
  10. android:id="@+id/topAppBar"
  11. android:layout_width="match_parent"
  12. android:layout_height="?attr/actionBarSize"
  13. android:background="@color/appTopBar"
  14. app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
  15. app:title="@string/topAppBarTitle_Settings" />
  16. </com.google.android.material.appbar.AppBarLayout>
  17. <FrameLayout
  18. android:id="@+id/settings"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent">
  21. </FrameLayout>
  22. </LinearLayout>

第二步:代替 topAppBar.setNavigationOnClickListener ,使用 topAppBar.setOnClickListener ```
View topAppBar = findViewById(R.id.topAppBar);
topAppBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Account.class);
startActivity(i);
}
});

展开查看全部

相关问题