2017-08-15 29 views
3

我想改变我的应用程序的默认动作条,我尝试这在我的MainActivity:Android的 - 不能改变动作条

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"> 
    <[email protected]/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> 

的问题是动作条不会改变。它仍然是一样的defult之一。

感谢您的帮助:)

回答

2

则需要使用设置以下

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

然后在MainActivity.java主题,在AndroidManifest.xml文件活动NoactionBar,而不是下面的两行

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

添加这些代码行

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

我试图使用android:theme =“@ style/AppTheme.NoActionBar”,但是我得到一个错误:“cannt解决符号'@style/AppTheme.NoActionBar' 然后我试着写@ style/Theme。 AppCompat.Light.NoActionBar,我并没有有任何错误,但随后一切都变得更暗,我看不出在上面的任何行动吧。 – Shayts

+0

在你style.xml文件中添加以下 <样式名称=” AppTheme.NoActionBar“> <项目名称=” windowActionBar“>假 <项目名称=” windowNoTitle“>真 –

+0

试过,但没有行动起来吧,在现在都... – Shayts

1

actionBar.setDisplayShowCustomEnabled(真);

设置自定义视图之前加入这一行。

+0

问题尝试这一点,但它让我看到我的粉红色的工具栏从XML文件,默认的操作栏上,有垫衬。所以我真正看到的是普通蓝色的​​粉色动作栏。 – Shayts

+0

你添加了这些行吗? 工具栏工具栏=(工具栏)findViewById(R.id.toolbar); setSupportActionBar(toolbar); –

+0

你的意思是这样吗? 工具条工具栏=(栏)findViewById(R.id.toolbar); android.support.v7.app.ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayShowCustomEnabled(true); setSupportActionBar(toolbar); – Shayts