简体   繁体   English

标题在自定义工具栏中不可见

[英]Title not visible in Custom Toolbar

I'm trying to implement a Custom Toolbar, but i cant manage to get the title in it.我正在尝试实现自定义工具栏,但我无法在其中获取标题。 I've tried many posts on StackOverflow but without succes.我在 StackOverflow 上尝试了很多帖子,但都没有成功。

Activity_Main.xml: Activity_Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar"
        android:id="@+id/toolbar"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!" />

</RelativeLayout>

Toolbar.xml工具栏.xml

<?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="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    >

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

AndroidManifest.xml AndroidManifest.xml

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

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

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

</manifest>

Styles.xml样式文件

<resources>

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

</resources>

MainActivity.java主活动.java

package com.example.diong.amyApp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}

I really tried a lot.我真的尝试了很多。

  • Trying to set the title using getSupportActionBar().setTitle(title);尝试使用 getSupportActionBar().setTitle(title); 设置标题;
  • I tried other pc's with new projects.我用新项目尝试了其他电脑。
  • I tried to user other methods我尝试使用其他方法

If someone could help me out that would be awesome!如果有人可以帮助我,那就太棒了!

UPDATE更新

I've found "a" fix, In the toolbar.xml add:我找到了“a”修复,在toolbar.xml中添加:

xmlns:app="http://schemas.android.com/apk/res-auto"
app:title="@string/app_name"

Now it works but.. I see that the code was suggested in the MainActivity.java is doing nothing现在它可以工作了,但是..我看到 MainActivity.java 中建议的代码没有做任何事情

Toolbar toolbar = findViewById(R.id.toolbar);

toolbar.setTitle("Your title here");

setSupportActionBar(toolbar);

Can someone help me out?有人可以帮我吗?

UPDATE 2.0更新 2.0

I found a error and i found out that the title is working fine on the virtual device or my phone, just not in the preview design我发现了一个错误,我发现标题在虚拟设备或我的手机上运行良好,只是在预览设计中没有

IDE Screenshot 1 IDE 截图 1

IDE Screenshot 2 IDE 截图 2

IDE Screenshot 3 IDE 截图 3

IDE Screenshot 4 IDE 截图 4

IDE Error Screenshot 5 IDE 错误截图 5

IDE Error Screenshot 6 IDE 错误截图 6

I think the problem is that your id toolbar is the id of your include tag, but not of the Toolbar .我认为问题在于您的 id toolbar是您的include标签的 ID,而不是Toolbar的 ID。 So try to delete the include tag and replace it with your Toolbar .因此,请尝试删除include标记并将其替换为您的Toolbar Dont forget to add the id to your Toolbar .不要忘记将 id 添加到您的Toolbar Then it should work.那么它应该工作。

Activity_Main.xml Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

     <android.support.v7.widget.Toolbar          
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@color/colorPrimary"
     android:elevation="4dp"
     android:id="@+id/toolbar"
      >

 </android.support.v7.widget.Toolbar>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Hello World!" />

  </RelativeLayout>

To change the title add getSupportActionBar.setTitle("Your title");要更改标题添加getSupportActionBar.setTitle("Your title"); to your MainActivity.到您的 MainActivity。

Take the id set inside include tag and put it inside Toolbar tag.include标签内的 id 设置并将其放入Toolbar标签内。 Here the include places the Toolbar.xml inside the Activity_Main.xml but does not affect the id to Toolbar .这里的 include 将Toolbar.xml放在Activity_Main.xml但不影响Toolbarid So your XMLs should be like this:所以你的 XML 应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!" />

</RelativeLayout>

And this for the toolbar:这对于工具栏:

<?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="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:id="@+id/toolbar"
    >

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

And remember to set your title, if you want it dynamically can proceed this way:记住设置你的标题,如果你想动态地可以这样进行:

package com.example.diong.amyApp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);

         toolbar.setTitle("Your title here");

        setSupportActionBar(toolbar);


    }
}

There is no way to get the real "live" title to appear in the IDE preview.没有办法让真正的“实时”标题出现在 IDE 预览中。

The preview doesn't know about the code executed in your activity's onCreate() method (even if you specify tools:context on your root view).预览不知道在您的活动的onCreate()方法中执行的代码(即使您在根视图上指定了tools:context )。 That means that the preview doesn't know that this Toolbar is your activity's action bar , and so it doesn't know that it should display the title.这意味着预览不知道这个Toolbar是你的 Activity 的action bar ,所以它不知道它应该显示标题。 Additionally, it won't know about any setTitle() calls made in your activity.此外,它不会知道您的活动中进行的任何setTitle()调用。

However, you could add tools:title to the toolbar itself, so that you see some text there.但是,您可以将tools:title添加到工具栏本身,以便您在那里看到一些文本。 That will show you how the title would appear, so you can see text color/size/etc in the preview.这将向您显示标题的显示方式,因此您可以在预览中看到文本颜色/大小/等。

From my point of view, you need to make changes in your code like:从我的角度来看,您需要对代码进行更改,例如:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(Html.fromHtml(
            "<font color='#673AB7'><b>Your Title</b></font>")

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

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