【问题标题】:Issues in customizing toolbar in AndroidAndroid中自定义工具栏的问题
【发布时间】:2017-09-26 13:59:44
【问题描述】:

我想完成以下任务

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

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

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

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

这里是 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>

这就是我的屏幕的样子

【问题讨论】:

    标签: android android-layout android-toolbar


    【解决方案1】:

    您需要解决相应的类,因此请使用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
    

    【讨论】:

    • 我进行了必要的更改。现在工具栏是空的,如何添加标题?
    • 你必须使用title属性`app:title="yourTitle"`
    • 我明白了!问题解决了 !非常感谢您的帮助!
    • 您可以在工具栏中进一步添加自己的 textView 而不是 app:title 以进一步操作它,如果您愿意,我很高兴能提供帮助,祝您编码愉快
    猜你喜欢
    • 1970-01-01
    • 2016-09-16
    • 2016-01-02
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多