ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android – 无法更改ActionBar

2019-06-27 16:11:30  阅读:208  来源: 互联网

标签:android android-actionbar


我正在尝试更改我的应用程序的默认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没有改变.它与defualt一样.

谢谢你的帮助:)

解决方法:

您需要使用以下内容将该活动的主题设置为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,android-actionbar
来源: https://codeday.me/bug/20190627/1306452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有