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

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

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

package studio.itztaylorau.dashboard;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

import studio.itztaylorau.dashboard.R;
import studio.itztaylorau.dashboard.AccountActivity;

public class SettingsActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        View topAppBar = findViewById(R.id.topAppBar);
        topAppBar.setNavigationOnClickListener {
            Intent i = new Intent(getApplicationContext(), Account.class);
            startActivity(i);
        }
    }
    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}

设置活动.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:id="@+id/settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topAppBar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/topAppBarTitle_Settings"
                android:background="@color/appTopBar"
                app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
                />
        </com.google.android.material.appbar.AppBarLayout>
    </FrameLayout>
</LinearLayout>
ewm0tg9j

ewm0tg9j1#

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/appTopBar"
            app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
            app:title="@string/topAppBarTitle_Settings" />
    </com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:id="@+id/settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>
</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);
}
});

相关问题