添加“向上”按钮但不在“第二个活动”的标题栏中显示

时间:2016-01-19 12:30:46

标签: android android-manifest

我正在尝试在第二个活动中添加按钮,但它不会显示我现在没有为什么...我尝试了不同的但仍然卡住了...请帮助.. 这是第二个活动的代码......

    package com.example.hamza.actionbarmenu;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;

public class secondActivity extends MainActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

            getActionBar().setDisplayHomeAsUpEnabled(true);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }


    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent myIntent = new Intent(this, MainActivity.class);
        startActivity(myIntent);
        return true;
    }
}

4 个答案:

答案 0 :(得分:2)

请尝试使用getSupportActionbar()代替getActionBar()

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

答案 1 :(得分:1)

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

你可以在清单文件中添加以下代码以进行顶级活动

 <activity
        android:name=".SecondActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />

    </activity>

答案 2 :(得分:0)

public class secondActivity extends MainActivity

尝试将以上行替换为:

public class secondActivity extends Activity

答案 3 :(得分:0)

尝试将parentActivity添加到清单中的SecondActivity,如下所示:

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

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <!-- Main Activity -->

            <activity android:name=".MainActivity">

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

            <!-- SecondActivity -->
            <activity
                android:name="SecondActivity"
       ===>         android:parentActivityName=".MainActivity">                      
             </activity>

        </application>

    </manifest>

并使用getSupportActionBar()代替getActionBar()