簡體   English   中英

如何從 Kotlin 的工具欄菜單中刪除標題?

[英]How to remove the title from toolbar menu in Kotlin?

我有這個工具欄

在此處輸入圖像描述

我想刪除片段的標題,以便顯示更多項目

是來自 xml 文件還是活動?

因為我試圖將此添加到我的主要活動中:

getActionBar().setDisplayShowTitleEnabled(false);

getSupportActionBar().setDisplayShowTitleEnabled(false);

setSupportActionBar(toolbar).setTitle("");

toolbar.setTitle(null)

但他們都沒有工作

有什么建議嗎?

這是 MainActiviy 代碼:

class MainActivity : AppCompatActivity() {











override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)
    









    val navController = Navigation.findNavController(this, R.id.nav_host_fragment)



    setupBottomNavMenu(navController)
    setupSideNavigationMenu(navController)
    setupActionBar(navController)

您可以為每個片段創建自己的工具欄,並根據您的要求對其進行自定義。 我制作了一個通用工具欄 xml 文件,並在每個片段中重用它並對其進行自定義。 看:

common_toolbar.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/const_item"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <TextView
        android:id="@+id/back_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="@dimen/margin_normal"
        android:gravity="center"
        android:padding="@dimen/padding_five"
        android:text="Back"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_size_fifteen"
        app:drawableStartCompat="@drawable/ic_action_chevron_left"
        app:drawableTint="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/title_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/menu"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_size_fifteen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>

然后將其添加到 Fragments 的 UI。 比方說,添加到 fragment_user_details.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/toolbar_common"
    android:id="@+id/toolbar_user_details"/>
<!-- 
 Other View widgets
 -->
</androidx.constraintlayout.widget.ConstraintLayout>

由於每個片段都有自己的工具欄,所以我通過它的 id 獲取它並根據我的要求進行自定義。 確保 Activity 具有Theme.MaterialComponents.Light.NoActionBar主題。

菜單項也可以如下添加:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

由於您使用的是導航組件,因此您可以使用addOnDestinationChangedListener在您的設置方法之后設置您的自定義標題。

navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.xxxx) {
       //If in your setup you are using a Toolbar
       toolbar.title = ""
       //supportActionBar?.title = "" //If you are using setSupportActionBar()
   } else {
       //
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM