简体   繁体   English

没有获得自定义工具栏标题

[英]Not getting Custom Toolbar Title

I tried various solution to get toolbar title but none of them worked.我尝试了各种解决方案来获取工具栏标题,但都没有奏效。 like using and others.喜欢使用等。 What went wrong with my code.我的代码出了什么问题。 can you advice.你能建议吗。 I m getting menu and their options but Title of toolbar no luck.我正在获取菜单及其选项,但工具栏的标题没有运气。

getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setTitle("Hello");

Below is my style.xml code:下面是我的 style.xml 代码:

<!-- 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>

My mainactivity code:我的主要活动代码:

package jss.customtoolbar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
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);
   getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle("Hello");

}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

menu.xml code: menu.xml 代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/miCompose"
        android:icon="@mipmap/ic_launcher"
        android:title="Compose"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/miProfile"
        android:title="Profile" />
</menu>

Toolbar code:工具栏代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:title="Hello">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">




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

and activity_main.xml code:和 activity_main.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="jss.customtoolbar.MainActivity">

    <include
        layout="@layout/tool"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

You can set title to toolbar for reference no need to get toolbar of support.您可以将标题设置为工具栏以供参考,无需获得支持的工具栏。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Hello");
    setSupportActionBar(toolbar);
}

Toolbar xml工具栏xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:title="Hello">
    </android.support.v7.widget.Toolbar>
</LinearLayout>

activity xml活动xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="jss.customtoolbar.MainActivity">

    <include
        layout="@layout/tool"/>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM