Android工作室操作栏未在新活动中显示

时间:2017-07-04 05:10:39

标签: java android eclipse-adt

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

在我的新活动中,我的动作栏不会显示,我不知道为什么会这样。它只出现在第一个活动上。并且它不会出现在任何其他活动中。我是Android开发的新手,所以这可能是一个问题。但它真的困扰我。我已经为我的项目包含了我的代码。谢谢你的阅读!

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.karanvir.search.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="59dp"
            android:text="AutoCompleteTextView" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/autoCompleteTextView"
            android:layout_marginLeft="12dp"
            android:layout_marginStart="12dp"
            android:layout_marginTop="48dp"
            android:layout_toEndOf="@+id/progressBar"
            android:layout_toRightOf="@+id/progressBar"
            android:onClick="jump"
            android:text="Button" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>
package com.karanvir.search;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static com.karanvir.search.MainActivity.urlGlobal;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //finding webview
        WebView webView=(WebView) findViewById(R.id.web1);

//whole bunch of settings you might want to do if you .
        //do this because javascript is so wildely used that if you dont use this anywebsites you display wont be displayed properly
        webView.getSettings().setJavaScriptEnabled(true);
        //this is because on a number of phones if you dont do this it will jump to the devices default browser, and ddisplay the websview their instead.
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://www.google.ca/?gws_rd=ssl#q="+urlGlobal);
        //reminder ask permission
        //you can load content using loaddata
        //then add type of data
        //then add character encoded were using

    }

}
package com.karanvir.search;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    Intent intentGoogle;
    Random rn;
    SharedPreferences urls;
    AutoCompleteTextView searchBar;
    public static String urlGlobal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        searchBar=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        Button button=(Button) findViewById(R.id.button);

         rn= new Random();

        urls=this.getSharedPreferences("com.karanvir.search", Context.MODE_PRIVATE);






    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings4) {
            Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(intentGoogle);



            return true;
        } else if(id ==R.id.action_settings2){
            Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
            startActivity(intentGoogle);


            return true;

        }else if (id==R.id.action_settings3){
            Intent intentGoogle= new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intentGoogle);


            return true;

        }else if(id==R.id.action_settings1){
            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.alert_dark_frame)
                    .setTitle("About")
                    .setMessage("stuff");
                    return true;

        }

        return super.onOptionsItemSelected(item);
    }


    public void jump(View view){
        //intnet changing target of our code
urlGlobal=searchBar.getText().toString();
        Log.i("stuff",urlGlobal);


        //public static String urlGlobal=




       /* urls.edit().putString("url",searchBar.getText().toString()).apply();
        String Stringurls=urls.getString("url","");*/

        int pageJump = rn.nextInt(3)+1;
        if (pageJump==1){
            //google
            Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(intentGoogle);
        } else if (pageJump==2){
            //YAHOO
            Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
            startActivity(intentGoogle);

        } else if(pageJump==3){
            //GOOGLE
            Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(intentGoogle);

        }


    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.karanvir.search">
    <uses-permission android:name="android.permission.INTERNET"/>

    <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=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Main2Activity"
            android:label="@string/title_activity_main2"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Yahoo"
            android:label="@string/title_activity_yahoo"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.karanvir.search.Main2Activity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/web1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

4 个答案:

答案 0 :(得分:2)

在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>

并像上面一样更新您的AndroidManifest文件

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.karanvir.search">
<uses-permission android:name="android.permission.INTERNET"/>

<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=".MainActivity"
        android:label="@string/app_name"
     >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Main2Activity"
        android:label="@string/title_activity_main2"
       />
    <activity
        android:name=".Yahoo"
        android:label="@string/title_activity_yahoo"
       ></activity>
</application>

答案 1 :(得分:1)

在styles.xml中添加

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/grey_400</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
  

在你的Manifest中,在每个Activity中添加它

<activity
        android:name=".YourActivity"
        android:label="@string/title_activity_main2"
        android:theme="@style/AppTheme" /> 

答案 2 :(得分:0)

尝试使用活动主题为AppTheme.NoActionBar,希望对您有帮助。

  <?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/activity_login"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <TextView
                    style="@style/Base.TextAppearance.Widget.AppCompat.Toolbar.Title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/login_title"
                    android:textColor="@color/colorPrimary"
                    />
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>




    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

答案 3 :(得分:0)

在使用Android Studio并添加新的Activity时,请尝试:

File -> New -> Activity -> Basic Activity 

然后将其默认配置为ActionBar。

enter image description here