样式化工具栏菜单项标题
Styling toolbar menu item title
我正在尝试实现菜单项设计,如下面的 YouTube 应用程序屏幕所示。我感兴趣的菜单项是操作菜单项。在这种情况下 (G)
目前,我的应用程序如下图所示:
我的风格和背景xml如下:
<resources>
// The themes are structured as follows :
// Theme 1 (One) : Application Theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="otpViewStyle">@style/OtpWidget.OtpView</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="actionMenuTextColor">@color/white</item>
<item name="android:actionMenuTextColor">@color/white</item>
<item name="actionMenuTextAppearance">@style/home_menu_style</item>
<item name="android:actionMenuTextAppearance" >@style/home_menu_style</item>
</style>
// Theme 2 (two) : Splash Screen Theme
<style name="SplashScreenTheme" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
// Theme 3 (three) : No ActionBar Theme
<style name="AppTheme.NoActionbar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
// Theme 4 (four) : AppBarOverlay and PopupOverlay
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Light" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />
// Menu Style
<style name="home_menu_style" parent="AppTheme">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/menu_drawable</item>
<item name="android:backgroundTint">@drawable/menu_drawable</item>
<item name="backgroundTint">@drawable/menu_drawable</item>
</style>
</resources>
menu_drawable如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#48b3ff">
</solid>
</shape>
home_menu.xml菜单项如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_name"
android:actionLayout="@layout/custom_layout"
android:title=""
app:showAsAction="always" />
<item
android:id="@+id/action_search"
android:icon="@drawable/sharp_search_white_24"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>
用于扩充菜单的代码:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
{
super.onCreateOptionsMenu(menu, inflater)
menu.clear()
this.menu = menu
inflater.inflate(R.menu.home_menu, menu)
}
我更改菜单项标题的代码如下:
var menu: Menu
menu.findItem(R.id.menu_name).setTitle(name)
菜单预览如下:
首先,您需要像这样创建一个 custom_menu.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/profile"
<!-- this is the magic !!! -->
android:actionLayout="@layout/custom_layout"
android:icon="@drawable/ic_action_profile"
android:title="@string/profile_update"
app:showAsAction="always" />
</menu>
在此之后,您创建一个布局文件 custom_layout.xml
,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="@dimen/margin_extra_small"
android:background="@drawable/drawable_circle_solid_accent"
android:gravity="center"
android:padding="@dimen/padding_small"
android:text="G"
android:textColor="@color/colorWhite"
android:textSize="@dimen/text_size_normal"
android:textStyle="bold" />
</LinearLayout>
并在您的 activity
中设置此 custom_menu.xml
。
输出:
将此添加到您的 activity
:
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
// getting action layout menu item
val menuItemName = menu?.findItem(R.id.menu_name)
// getting Linear Layout from custom layout
val linearLayout = menuItemName?.actionView as LinearLayout
// getting TextView from Linear Layout, i.e., parent of custom layout
val yourTextView = linearLayout.findViewById<TextView>(R.id.your_text_view_id)
// setting the first char of the String name
yourTextView.text = name.substring(0, 1)
return super.onPrepareOptionsMenu(menu)
}
我正在尝试实现菜单项设计,如下面的 YouTube 应用程序屏幕所示。我感兴趣的菜单项是操作菜单项。在这种情况下 (G)
目前,我的应用程序如下图所示:
我的风格和背景xml如下:
<resources>
// The themes are structured as follows :
// Theme 1 (One) : Application Theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="otpViewStyle">@style/OtpWidget.OtpView</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="actionMenuTextColor">@color/white</item>
<item name="android:actionMenuTextColor">@color/white</item>
<item name="actionMenuTextAppearance">@style/home_menu_style</item>
<item name="android:actionMenuTextAppearance" >@style/home_menu_style</item>
</style>
// Theme 2 (two) : Splash Screen Theme
<style name="SplashScreenTheme" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
// Theme 3 (three) : No ActionBar Theme
<style name="AppTheme.NoActionbar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
// Theme 4 (four) : AppBarOverlay and PopupOverlay
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Light" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />
// Menu Style
<style name="home_menu_style" parent="AppTheme">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/menu_drawable</item>
<item name="android:backgroundTint">@drawable/menu_drawable</item>
<item name="backgroundTint">@drawable/menu_drawable</item>
</style>
</resources>
menu_drawable如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#48b3ff">
</solid>
</shape>
home_menu.xml菜单项如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_name"
android:actionLayout="@layout/custom_layout"
android:title=""
app:showAsAction="always" />
<item
android:id="@+id/action_search"
android:icon="@drawable/sharp_search_white_24"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>
用于扩充菜单的代码:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
{
super.onCreateOptionsMenu(menu, inflater)
menu.clear()
this.menu = menu
inflater.inflate(R.menu.home_menu, menu)
}
我更改菜单项标题的代码如下:
var menu: Menu
menu.findItem(R.id.menu_name).setTitle(name)
菜单预览如下:
首先,您需要像这样创建一个 custom_menu.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/profile"
<!-- this is the magic !!! -->
android:actionLayout="@layout/custom_layout"
android:icon="@drawable/ic_action_profile"
android:title="@string/profile_update"
app:showAsAction="always" />
</menu>
在此之后,您创建一个布局文件 custom_layout.xml
,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="@dimen/margin_extra_small"
android:background="@drawable/drawable_circle_solid_accent"
android:gravity="center"
android:padding="@dimen/padding_small"
android:text="G"
android:textColor="@color/colorWhite"
android:textSize="@dimen/text_size_normal"
android:textStyle="bold" />
</LinearLayout>
并在您的 activity
中设置此 custom_menu.xml
。
输出:
将此添加到您的 activity
:
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
// getting action layout menu item
val menuItemName = menu?.findItem(R.id.menu_name)
// getting Linear Layout from custom layout
val linearLayout = menuItemName?.actionView as LinearLayout
// getting TextView from Linear Layout, i.e., parent of custom layout
val yourTextView = linearLayout.findViewById<TextView>(R.id.your_text_view_id)
// setting the first char of the String name
yourTextView.text = name.substring(0, 1)
return super.onPrepareOptionsMenu(menu)
}