【问题标题】:Android - Can't change ActionBarAndroid - 无法更改 ActionBar
【发布时间】:2018-09-12 13:50:59
【问题描述】:

我正在尝试更改我的应用的默认 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);
}

这是我想要代替常规 ActionBartrytopbarleagues.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 没有改变。它与默认的相同。

感谢您的帮助:)

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    您需要在 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,我没有收到任何错误,但后来一切都变暗了,我在顶部看不到任何操作栏。
    • 在您的 style.xml 文件中添加以下
    • 完全没有操作栏是什么意思?你能发布清单文件和活动文件内容吗?
    • 我已经上传了清单并编辑了 mainActivity。它在上面的问题中
    【解决方案2】:
    actionBar.setDisplayShowCustomEnabled(true);
    

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

    【讨论】:

    • 试试看,但它会在默认操作栏上向我显示 xml 文件中的粉色工具栏,并带有填充。所以我实际看到的是普通蓝色操作栏上的粉红色操作栏。
    • 你添加了这些行吗?工具栏工具栏 = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(工具栏);
    • 你的意思是这样吗?工具栏工具栏 = (Toolbar) findViewById(R.id.toolbar); android.support.v7.app.ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayShowCustomEnabled(true); setSupportActionBar(工具栏);
    • 尝试将此行移到末尾 mActionBar.setDisplayShowCustomEnabled(true);然后设置自定义视图。
    • 然后我有这个例外:“这个活动已经有一个由窗口装饰提供的操作栏。不要请求 Window.FEATURE_SUPPORT_ACTION_BAR 并在你的主题中将 windowActionBar 设置为 false 以使用工具栏。”
    猜你喜欢
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多