Android – actionbar cannot be changed

I am trying to change the default actionbar of my application. I try this in my 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 I want, not the regular 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:

<?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 actionbar hasn't changed. It's the same as default

Thank you for your help:)

resolvent:

You need to set the theme of the activity to noactionbar in the androidmanifest.xml file using the following

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

Then in mainactivity. Java instead of the following two lines

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);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>