簡體   English   中英

Android Studio操作欄未顯示在新活動中

[英]Android studio action bar not showing in new activity

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開發的新手,所以這可能是anoob問題。 但這真的讓我感到困擾。 我已經為我的項目添加了代碼。 感謝您的閱讀!

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

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

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

然后在清單中的每個活動中添加

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

嘗試將活動主題設置為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>

當您使用Android Studio並添加新的Activity時,請嘗試:

File -> New -> Activity -> Basic Activity 

然后,它具有默認配置的ActionBar。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM