简体   繁体   中英

Back arrow isn't shown in @layout/toolbar markup

I have a problem. I wanted to create a back arrow, but it doesn't show. The title also doesn't show. I created the toolbar with an extra.xml file. So I hope you can help me, guys. Thanks in advance.

These are my files: Files This is my standard screen: Standard screen This is my settings screen: setting screen

SettingsActivity.java

    public class SettingsActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("My title");
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            int id = item.getItemId();
            if (id == android.R.id.home)
            {
                onBackPressed();
                return true;
            }
            else
            {
                return super.onOptionsItemSelected(item);
            }
        }

    }

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

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Settings!"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_info"
        android:icon="@drawable/ic_info_white_24dp"
        android:title="Info"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/menu_settings"
        android:icon="@drawable/ic_settings_white_24dp"
        android:title="Settings"
        app:showAsAction="ifRoom"/>
</menu>

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.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>
    <style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
        <item name="android:src">@drawable/ic_settings_white_24dp</item>
        <item name="android:contentDescription">"Lala"</item>
    </style>

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SettingsActivity"
            android:parentActivityName=".MainActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    app:titleTextColor="#fff">


</androidx.appcompat.widget.Toolbar>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_info:
                Toast.makeText(this, "Info", Toast.LENGTH_LONG).show();
                return true;
            case R.id.menu_settings:
                setContentView(R.layout.activity_settings);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Hi Change the following in your code

Add this line to setting class setSupportActionBar(toolbar);

I never called the right SettingsActivity.java

So change

setContentView(R.layout.activity_settings);

to

Intent intentAccess = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intentAccess);
 Toolbar mToolbar = findViewById(R.id.toolbar);
        (AppCompatActivity) setSupportActionBar(mToolbar);
        (AppCompatActivity) getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        (AppCompatActivity) getSupportActionBar().setDisplayShowHomeEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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