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

时间:2017-01-22 15:20:13

标签: java android android-toolbar

我尝试了各种解决方案以获得工具栏标题,但它们都没有奏效。喜欢使用和其他人。我的代码出了什么问题。你能建议吗?我得到菜单和他们的选项,但工具栏的标题没有运气。

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);
    }
}

下面是我的style.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>

我的主要活动代码:

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

menu.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>

工具栏代码:

 $string= 'This is a string? Or an array?';
 $array = array_filter(array_map('trim',preg_split("/\b/", $string)));
 print_r($array);

和activity_main.xml代码:

Array
(
    [1] => This
    [3] => is
    [5] => a
    [7] => string
    [8] => ?
    [9] => Or
    [11] => an
    [13] => array
    [14] => ?
)

1 个答案:

答案 0 :(得分:0)

您可以将标题设置为工具栏以供参考,无需获得支持工具栏。

 @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);
}

工具栏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>

活动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>