关于Toolbar 的一点问题

1、继承AppcompatActivity,使用toolbar时,toolbar 的setTitle等操作都需要在setSupportActionBar
之前调用,否则结果跟没有 setSupportActionBar之前一模一样,

同时,如果toolbar里面没有内容,即使设置了

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

并且在 Manifest中使用 AppTheme.NoActionBar 也没有效果,结果还是和没有 setSuppoutActionBar之前一模一样,如果有xml中有包含内容,或者代码中有出现 toolbar 的内容添加,才会出现toolbar的效果
也就是说:
Toolbar 在 xml 或者 java 代码中要有设置内容,才会有toolbar效果,并且 java 代码中 setTitile 等对Toolbar 的操作要在 setSupportActionBar 之前。

2、toolbar 设置样式时如果 有toolbar 的Activity 在manifest中使用上面的 Theme.NoActionBar,在该activity 中的 textView 设置 textColor 会无效

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height = "?attr/actionBarSize"
            android:background="?attr/colorPrimary">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="小明"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize = "20sp"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    ***
    ***
     <Button
         android:text="发 送"
         android:textColor="@android:color/white"/>

结果如下:
关于Toolbar 的一点问题_第1张图片

图片有点大,把 style 修改一下,改成如下

<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
       <item name="android:textColorPrimary">@android:color/white</item>
 </style>

同时将 AppTheme.NoActionBar 修改成 AppTheme2,结果如下:

关于Toolbar 的一点问题_第2张图片

发现按钮文字颜色终于改变了,然后文字“小明”并不是居中的,在java 中设置 setTitle(“”),这里是空字符串,然后在Toolbar中添加 TextView ,即上面添加的 “小明”,设置为居中,即使用了 textView 代替了 title,并且设置为 layout_gravity = “center ” 小明就可以居中了,同时,返回箭头和小明字体都是黑色的,设置如下即可修改为 白色:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height = "?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            <TextView
                android:layout_centerInParent = "true"
                android:text="小明"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_gravity = "center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar>

添加了一个 app:theme ,这里使用的是 AppTheme2 结果如下:
关于Toolbar 的一点问题_第3张图片

将主题改成 AppTheme.NoActionBar,即之前说使用了这个之后单纯修改color 无效,在添加了app:Theme 之后,便可以了,所以此时可以使用 AppTheme.NoActionBar,结果如下:

关于Toolbar 的一点问题_第4张图片

你可能感兴趣的:(android)