简体   繁体   English

Android - 无法更改ActionBar

[英]Android - Can't change ActionBar

I'm trying to change the default ActionBar of my app, and i try this in my MainActivity: 我正在尝试更改我的应用程序的默认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);
}

This is the layout file that i want to be instead of the regular ActionBar- trytopbarleagues.xml: 这是我想要的布局文件,而不是常规的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 Manifest: 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>

The problem is that the ActionBar doesn't change. 问题是ActionBar没有改变。 it remains the same as the defualt one. 它与defualt一样。

Thanks for helping :) 谢谢你的帮助:)

You need to set the theme to that activity to NoactionBar in the AndroidManifest.xml file using the following 您需要使用以下内容将该活动的主题设置为AndroidManifest.xml文件中的NoactionBar

android:theme="@style/AppTheme.NoActionBar"

Then in the MainActivity.java, instead of the following two lines 然后在MainActivity.java中,而不是以下两行

android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.trytopbarleagues);

add these lines of code 添加这些代码行

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar.setDisplayShowCustomEnabled(true);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM