【问题标题】:How do I change the Background Color of a Lollipop Toolbar?如何更改棒棒糖工具栏的背景颜色?
【发布时间】:2015-01-06 14:50:12
【问题描述】:

如何在 Xamarin Studio 中更改 Android Lollipop Toolbar 的背景颜色?

我尝试了许多不同的方法,但都没有成功,但这是我目前所拥有的,但它不起作用。密切注意图像。出于某种原因,工具栏的底部是彩色的(阴影应该在哪里),而不是实际工具栏的背景:

添加所有样式和所有内容后,在可视化设计器中,我看到的只有这个(没有颜色背景):

我的代码:

styles.xml(直接位于资源文件夹下):

<?xml version="1.0" encoding="UTF-8" ?>
 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ActionBarThemeOverlay"
        ndroid:background="?attr/colorPrimaryDark" />

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="#ffffffff">
    <Toolbar
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:theme="@android:style/Theme.Material.Light" />
</LinearLayout>

Main.xml(资源 > 菜单 > Main.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto">
        <item android:icon="@android:drawable/ic_menu_share" android:id="@+id/menu_share" android:title="Share" android:showAsAction="ifRoom" />
</menu>

有人知道如何更改工具栏的背景颜色吗?


更新:


Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="#ffffff">
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="?actionBarSize"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:background="?attr/colorPrimary" />
</LinearLayout>

颜色.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
   <!-- Color : Classic colors -->
   <color name="white">#FFFFFF</color>
   <color name="black">#000000</color>
</resources>

styles.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="AppTheme" parent="AppTheme.Base"/>
  <style name="AppTheme.Base" parent="Theme.AppCompat">
    <!-- Value : Color -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorAccent">@color/black</item>
  </style>
</resources>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="ToolbarSample.ToolbarSample">
    <uses-sdk />
    <application android:label="ToolbarSample" android:theme="@style/AppTheme"></application>
</manifest>

【问题讨论】:

  • 我认为Toolbar 遵循了您应用程序主题中的android:colorPrimary。
  • @shkschneider 我不知道该放在哪里或如何放置。你能详细说明一下吗?
  • @shkschneider 我认为该页面上的代码 sn-p 应该进入 styles.xml 文件?我现在已经这样做了,并添加了我自己的颜色 (#26B0FB),但工具栏颜色仍然相同。
  • 您是否在AndroidManifest.xml 文件中链接了您的主题?在application 标签中使用android:theme=""。所有这些都是基本的 android 主题...

标签: android xml xamarin xamarin.android xamarin-studio


【解决方案1】:

工具栏就像一个视图,所以你可以在Main.xml中这样改变他的背景颜色:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="?actionBarSize"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:background="@color/black" />

您必须在文件 color.xml 中定义此颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <!-- Color : Classic colors -->
   <color name="white">#FFFFFF</color>
   <color name="black">#000000</color>
</resources>

如果你真的想使用android:colorPrimary。 您可以在文件styles.xml 中创建自己的主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="AppTheme" parent="AppTheme.Base"/>
  <style name="AppTheme.Base" parent="Theme.AppCompat">
    <!-- Value : Color -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorAccent">@color/black</item>
  </style>
</resources>

改用android:background="?attr/colorPrimary"。

顺便说一句,要在你所有的活动中使用这个主题,你必须在你的AndroidManifest.xml 中添加android:theme 来声明他:

<application
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >

希望这个解释清楚。

祝你有美好的一天!

【讨论】:

  • 谢谢@LeMimit,但我收到错误:“C:\Users\Jase\Documents\Projects\ToolbarSample\ToolbarSample\Resources\layout\main.xml(0,0):错误:否找到与给定名称匹配的资源(在 'background' 中,值为 '?attr/colorPrimary')。(ToolbarSample)“但我把所有东西都放在那里了。
  • 如果您使用android:background="@color/black" 的解决方案,您不使用您的主题来定义工具栏的背景颜色?
  • 如果我使用该解决方案,它不会将颜色更改为黑色。没有任何变化。
  • 真的很奇怪,我这样改了,它对我很好:(
  • 您可以重新发布您所做的更改的文件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 2015-03-06
  • 2015-08-14
  • 2015-09-13
  • 2017-10-16
  • 1970-01-01
相关资源
最近更新 更多