应用栏标题不显示在Android 5.0及更高版本中

时间:2016-08-05 11:14:34

标签: java android xml material-design

您好我已经创建了一个简单的应用程序并在Android 4.4.4及更高版本上进行了测试

在4.4.4 Kitkat中 - 它工作正常(我可以在应用栏中看到标题)

enter image description here

但是当我在Android 5.0及更高版本上测试它时,标题是不可见的

enter image description here

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimaryDark</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>


    <style name="myCustomAppBarTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
        <item name="android:textColorSecondary">@color/colorPrimary</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


    <!--*************************    Custom Theme  *******************************  -->
    <style name="myCustomAppBarTheme_old" parent="Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
        <item name="actionBarStyle">@style/AppTheme.ActionBar</item>
    </style>

    <style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
        <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleText</item>
        <item name="titleTextStyle">@style/AppTheme.ActionBar.TitleText</item>
        <item name="android:height">100dp</item>
        <item name="height">100dp</item>
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    </style>

    <style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/colorWhite</item>
    </style>


    <!--****************************  Custom Theme  *******************************  -->



    <!--****************************  Switch Theme  *******************************  -->
    <style name="SCBSwitch" parent="Theme.AppCompat.Light">
        <!-- active thumb & track color (30% transparency) -->
        <item name="colorControlActivated">@color/colorPrimaryDark</item>

        <!-- inactive thumb color -->
        <item name="colorSwitchThumbNormal">#f1f1f1
        </item>

        <!-- inactive track color (30% transparency) -->
        <item name="android:colorForeground">#42221f1f
        </item>
    </style>
    <!--****************************  Switch Theme  *******************************  -->

</resources>

Dashboard.xml活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/act_layou_dash_main"
    android:background="@color/colorWhite">

    <!-- Include Appbar -->
    <include
        android:id="@+id/app_bar_dashboard"
        layout="@layout/app_bar_dashboard" />

    <!-- include Shadow below appbar -->
    <View android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/toolbar_dropshadow"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <RelativeLayout
            android:id="@+id/relativemain_main"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view_tickets"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:scrollbars="none" />

        </RelativeLayout>



        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:layout_width="@dimen/navdrawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout = "@layout/fragment_navigation_drawer"
            android:name="baman.lankahomes.lk.zupportdeskticketsystem.Data.navigationDrawerFragment"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

App Bar信息中心

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorWhite"
    app:theme="@style/myCustomAppBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">



</android.support.v7.widget.Toolbar>

DashBoard类

public class Dashboard extends AppCompatActivity {
    private Toolbar toolbar;
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    private navigationDrawerFragment drawerFragment;
    List<TicketsItemObject> items = new ArrayList<TicketsItemObject>();

    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private TicketsRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Dashboard");
        setContentView(R.layout.activity_dashboard);

        applicationEnvironment = new ApplicationEnvironmentURL(this.context);
        context = this.getApplicationContext();

        toolbar = (Toolbar) findViewById(R.id.app_bar_dashboard);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        drawerFragment = (navigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setup(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="baman.lankahomes.lk">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Login"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Signup" />
        <activity
            android:name=".ForgotPassword"
            android:label="@string/forgotpass" />
        <activity
            android:name=".CompanyProfile"
            android:label="@string/your_company_profile" />
        <activity
            android:name=".Dashboard"
            android:launchMode="singleInstance" />
        <activity
            android:name=".NewTicket"
            android:launchMode="singleInstance" />
        <activity android:name=".Tickets" />
        <activity
            android:name=".TicketDetails"
            android:launchMode="singleInstance" />
        <activity
            android:name=".TicketResponse"
            android:launchMode="singleInstance" />
        <activity
            android:name=".AddNote"
            android:launchMode="singleInstance" />
        <activity android:name=".Notifications"
            android:launchMode="singleInstance" ></activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:1)

我认为你打电话后需要设置标题:

setSupportActionBar(toolbar);

然后使用:

getSupportActionBar().setTitle("title");

作为设置标题方法。

答案 1 :(得分:0)

Try to use:

getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Your title");

答案 2 :(得分:0)

getSupportActionBar().setTitle("title");

会做到这一点。

答案 3 :(得分:0)

我有一个类似的问题,但是有一个影响任何版本的来源。对getSupportActionBar().setTitle("title");的调用对我而言不起作用,因为在我的布局中,我已经在<android.support.v7.widget.Toolbar><RelativeLayout>中嵌套了一些位置。看来,在工具栏中插入您自己的嵌套布局会替换默认布局容器中的任何内容,因此也会删除默认标题视图。

因此,如果我想使用该布局,则必须提供并设置自己的TextView,它也要使用或覆盖style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"。最后,事实证明,我不需要自己的嵌套布局;我可以只使用android:layout_gravity和一些边距调整功能来将自定义视图放置在Toolbar中。