Android - 无法更改ActionBar

4

我想要改变我的应用程序的默认ActionBar,我尝试在MainActivity中进行以下操作:

    package com.example.shaytsabar.footballtables.activities;

import android.app.ActionBar;
import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.LoginFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.shaytsabar.footballtables.R;
import com.example.shaytsabar.footballtables.fragments.TableStandingsFragment;
import com.example.shaytsabar.footballtables.fragments.TopBarLeaguesFragment;

public class MainActivity extends AppCompatActivity{

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

这是我想要代替常规ActionBar的布局文件- trytopbarleagues.xml:
<?xml version="1.0" encoding="utf-8"?
 <android.support.constraint.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="wrap_content">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@color/colorAccent"
    android:minHeight="?attr/actionBarSize"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

  </android.support.v7.widget.Toolbar>
  </android.support.constraint.ConstraintLayout>

Android清单文件:

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

<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.NoActionBar">
    <!--@style/Theme.AppCompat.Light.NoActionBar AppTheme -->
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.intent.action.VIEW"/>

            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

            <!-- Accepts URIs that begin with "java-lang-
            programming://android-app-google-plus-demo" -->
            <data
                android:host="java-lang-programming"
                android:scheme="android-app-google-plus-demo"/>
        </intent-filter>
    </activity>
</application>
</manifest>

问题在于ActionBar没有改变,它仍然是默认的那个。谢谢你的帮助 :)
2个回答

2
您需要在AndroidManifest.xml文件中将主题设置为NoactionBar,使用以下内容:
android:theme="@style/AppTheme.NoActionBar"

在MainActivity.java中,不要使用以下两行代码:
android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.trytopbarleagues);

添加以下代码行

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

我尝试使用 android:theme="@style/AppTheme.NoActionBar",但是出现了错误:"无法解析符号 '@style/AppTheme.NoActionBar'。 然后我尝试编写 @style/Theme.AppCompat.Light.NoActionBar,就没有出现任何错误,但是接着整个界面变得更暗,我看不到顶部的操作栏。 - Shayts
在你的style.xml文件中添加以下内容: - Saila Lopa
你的意思是完全没有操作栏吗?你能发布清单文件和活动文件的内容吗? - Saila Lopa
我已经上传了清单文件并编辑了主活动。它在顶部的问题中。 - Shayts

1
actionBar.setDisplayShowCustomEnabled(true);

在设置自定义视图之前添加此行。

尝试了一下,但它显示了来自XML文件的粉色工具栏,在默认操作栏上,带有填充。所以我实际看到的是常规蓝色操作栏上的粉色操作栏。 - Shayts
你添加了这些行吗? Toolbar toolbar =(Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); - Levon Petrosyan
你的意思是这样吗?Toolbar toolbar =(Toolbar)findViewById(R.id.toolbar); android.support.v7.app.ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayShowCustomEnabled(true); setSupportActionBar(toolbar); - Shayts
尝试将此行代码移动到末尾: mActionBar.setDisplayShowCustomEnabled(true); 然后设置自定义视图。 - Levon Petrosyan
然后我遇到了这个异常:“此Activity已经有一个由窗口装饰提供的操作栏。不要请求Window.FEATURE_SUPPORT_ACTION_BAR并在您的主题中将windowActionBar设置为false,以使用Toolbar代替。” - Shayts
https://dev59.com/bV0a5IYBdhLWcg3wJVvv - Levon Petrosyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接