2017-06-12 44 views
2

应用程序名称未显示在应用程序栏中。它的白色。我不知道为什么。请帮忙。它在之前显示。然后我做了很多的变化,现在无法修复它为什么应用程序名称不会显示在Android的工具栏中?

在主要活动

.... 
<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    android:visibility="visible" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 


    .... 

</RelativeLayout> 

在风格

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

在Android清单

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

<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/Theme.AppCompat.Light.NoActionBar" 
    > 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

public class MainActivity extends AppCompatActivity { 

    public static final String LOG_TAG = MainActivity.class.getName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
     setSupportActionBar(myToolbar); 

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the view pager that will allow the user to swipe between fragments 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 

     // Create an adapter that knows which fragment should be shown on each page 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 

     // Set the adapter onto the view pager 
     viewPager.setAdapter(adapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 

     // Connect the tab layout with the view pager. This will 
     // 1. Update the tab layout when the view pager is swiped 
     // 2. Update the view pager when a tab is selected 
     // 3. Set the tab layout's tab names with the view pager's adapter's titles 
     //  by calling onPageTitle() 
     tabLayout.setupWithViewPager(viewPager); 

    } 
} 
+0

您是否在res> values> strings中指定了应用程序名称 –

+2

您需要将'myToolbar'初始化和'setSupportActionBar()'调用移至'setContentView()'后面。 –

回答

2

无论是在你的java文件中添加这一点。它为显示标题栏属性设置true,使其可见。

getSupportActionBar().setDisplayShowTitleEnabled(true); 

或者在你的XML工具栏元素

app:title="@string/app_name" 

app下添加此用来表示包括在支持库中的控件的属性。

最重要的是你应该有app_name字符串在你的string.xml文件中。

为标题在中心

你必须设计自己的自定义布局操作栏,并设置了这一点。 How to set title in centre in ActionBar?

+0

谢谢。你知道如何对齐中心的文字吗? – ams92

+0

回答编辑。看一看。 –

0

你可以做这样的..

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
setSupportActionBar(myToolbar); 
myToolbar.setTitle("Your App Name"); 
0

在风格

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> </style> 

这是你的问题。你把NoActionBar,这意味着它不会在顶部的酒吧。

你应该把DarkActionBar有你做更改之前的默认ActionBar。

希望它有帮助!

+0

应用程序崩溃 - 根据https://developer.android.com/training/appbar/setting-up.html我们必须添加它。使用这些主题之一可以防止应用程序使用本机ActionBar类来提供应用程序栏。 – ams92

相关问题