[英]Title not visible in Custom Toolbar
我正在尝试实现自定义工具栏,但我无法在其中获取标题。 我在 StackOverflow 上尝试了很多帖子,但都没有成功。
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>
工具栏.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
<?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>
样式文件
<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>
主活动.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);
}
}
我真的尝试了很多。
如果有人可以帮助我,那就太棒了!
更新
我找到了“a”修复,在toolbar.xml中添加:
xmlns:app="http://schemas.android.com/apk/res-auto"
app:title="@string/app_name"
现在它可以工作了,但是..我看到 MainActivity.java 中建议的代码没有做任何事情
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Your title here");
setSupportActionBar(toolbar);
有人可以帮我吗?
更新 2.0
我发现了一个错误,我发现标题在虚拟设备或我的手机上运行良好,只是在预览设计中没有
我认为问题在于您的 id toolbar
是您的include
标签的 ID,而不是Toolbar
的 ID。 因此,请尝试删除include
标记并将其替换为您的Toolbar
。 不要忘记将 id 添加到您的Toolbar
。 那么它应该工作。
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>
要更改标题添加
getSupportActionBar.setTitle("Your title");
到您的 MainActivity。
取include
标签内的 id 设置并将其放入Toolbar
标签内。 这里的 include 将Toolbar.xml
放在Activity_Main.xml
但不影响Toolbar
的id
。 所以你的 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>
这对于工具栏:
<?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>
记住设置你的标题,如果你想动态地可以这样进行:
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);
}
}
没有办法让真正的“实时”标题出现在 IDE 预览中。
预览不知道在您的活动的onCreate()
方法中执行的代码(即使您在根视图上指定了tools:context
)。 这意味着预览不知道这个Toolbar
是你的 Activity 的action bar ,所以它不知道它应该显示标题。 此外,它不会知道您的活动中进行的任何setTitle()
调用。
但是,您可以将tools:title
添加到工具栏本身,以便您在那里看到一些文本。 这将向您显示标题的显示方式,因此您可以在预览中看到文本颜色/大小/等。
从我的角度来看,您需要对代码进行更改,例如:
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.