[英]How to remove the title from toolbar menu in Kotlin?
i have this toolbar我有这个工具栏
i want to remove the title of the fragment so i can display more items我想删除片段的标题,以便显示更多项目
is it from the xml file or the activity?是来自 xml 文件还是活动?
because i tried to add this to my main activity:因为我试图将此添加到我的主要活动中:
getActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setSupportActionBar(toolbar).setTitle("");
toolbar.setTitle(null)
but none of them worked但他们都没有工作
any suggestion?有什么建议吗?
this is MainActiviy code:这是 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)
You could create your own tool bar for each fragment and customize it as per your requirement.您可以为每个片段创建自己的工具栏,并根据您的要求对其进行自定义。 I have made a common toolbar xml file and reusing it in every fragment and customizing it.
我制作了一个通用工具栏 xml 文件,并在每个片段中重用它并对其进行自定义。 Look:
看:
common_toolbar.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>
And then adding it to fragments's UI.然后将其添加到 Fragments 的 UI。 Let's say, adding to fragment_user_details.xml
比方说,添加到 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>
As each fragment has its own tool bar, so I am getting it by it's id and customize as per my requirement.由于每个片段都有自己的工具栏,所以我通过它的 id 获取它并根据我的要求进行自定义。 Make sure Activity has Theme.MaterialComponents.Light.NoActionBar theme.
确保 Activity 具有Theme.MaterialComponents.Light.NoActionBar主题。
Menu Items can also be added like below:菜单项也可以如下添加:
@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;
}
Since you are using a Navigation component you can use the addOnDestinationChangedListener
to set your custom title after your setup method.由于您使用的是导航组件,因此您可以使用
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.