android - 在 Android 中自定义工具栏的问题

标签 android android-layout android-toolbar

我想完成以下任务

  • 删除默认工具栏
  • 构建自定义工具栏
  • 更改工具栏中的默认标题、标题文本颜色、标题文本对齐方式

我成功删除了默认工具栏,但在构建自定义工具栏时我无法删除

  • 更改标题文字颜色
  • 更改标题文本对齐方式

我在堆栈溢出时尝试了针对上述问题提供的现有解决方案,但似乎对我的情况没有任何帮助

这里是toolBar.xml文件

enter code here
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbarC"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"

android:theme="@style/Mystyle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#ffffff">


</android.support.v7.widget.toolbar>

这是我的主要 xml 文件

enter code here`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:layout="http://schemas.android.com/apk/res-auto"
android:background="#3dcc24">

<include
layout="@layout/clubs_tool_bar" />

</LinearLayout>

这是我的 styles.xml 文件

<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>
    <item name="ColorTemp">@color/ColorTemp</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" 
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="Mystyle" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name = "android:textSize">30sp</item>



</style>
</resources>

enter image description here

我的屏幕是这样的

最佳答案

您需要解决相应的类,因此使用 android.support.v7.widget.Toolbar

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbarC"

    app:title="YourTitle"

    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"    
    android:theme="@style/Mystyle"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="#ffffff"/>

代替

android.support.v7.widget.toolbar

关于android - 在 Android 中自定义工具栏的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46428517/

相关文章:

android ListView 创建一个 3 列 View

android - 设置工具栏标题

android - View 与工具栏重叠

android - 如何延迟播放 AudioRecord

java - Android: fragment findViewById 内的选项卡返回 null?

java - 屏幕尺寸不同? (景观)

android - Chrome 自定义标签。设置多个工具栏项

android - 创建在固定时间段后过期的 Android 试用应用程序

java - 在 android 中使用 php-cgi 和网络服务器

安卓 : Scrolling in Grid layout?