Android 应用AppCompat主题

示例

AppCompat支持库提供了主题,以使用Material Design规范构建应用。Theme.AppCompat活动的扩展还需要带有父主题的主题AppCompatActivity。

第一步是自定义主题的调色板,以自动为应用程序着色。
在您的应用程序中,res/styles.xml您可以定义:

<!-- inherit from the AppCompat theme -->
<style name="AppTheme" parent="Theme.AppCompat">

    <!-- your app branding color for the app bar -->
    <item name="colorPrimary">#2196f3</item>
    
    <!-- darker variant for the status bar and contextual app bars -->
    <item name="colorPrimaryDark">#1976d2</item>

    <!-- theme UI controls like checkboxes and text fields -->
    <item name="colorAccent">#f44336</item>
</style>

Theme.AppCompat您也可以使用Theme.AppCompat.Light或代替背景较暗的图片Theme.AppCompat.Light.DarkActionBar。

您可以使用自己的颜色自定义主题。在“材料设计规范”颜色图表和“材料调色板”中可以找到不错的选择。“ 500”颜色是主色的不错选择(在此示例中为蓝色500);选择相同色调的“ 700”作为深色;以及与强调色不同的色调的阴影。主色用于应用程序的工具栏及其在概述(最近的应用程序)屏幕中的输入,深色用于在状态栏上着色,而强调色用于在某些控件上突出显示。

创建此主题之后,将其应用到中的应用中,AndroidManifest.xml并将该主题也应用到任何特定活动中。这对于应用AppTheme.NoActionBar主题很有用,可让您实现非默认工具栏配置。

<application android:theme="@style/AppTheme" 
    ...>
    <activity 
        android:name=".MainActivity"
        android:theme="@style/AppTheme" />
</application>

您还可以使用android:theme和ThemeOverlay主题将主题应用于各个视图。例如Toolbar:

<android.support.v7.widget.Toolbar
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?attr/colorPrimary"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

或Button:

<Button
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/MyButtonTheme"/>

<!-- res/values/themes.xml -->
<style name="MyButtonTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">@color/my_color</item>
</style>