[英]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.