如何创建自定义主题并在Android应用程序中使用它? [英] How to create a custom theme and use it in an Android application?

查看:64
本文介绍了如何创建自定义主题并在Android应用程序中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建自定义主题并在代码中使用它?

How to create a custom themes and use it in the code?

在菜单中如何实现主题选项并申请活动?

In menu how to implement theme option and apply for the activity?

推荐答案

有一个不错的样式和主题指南" .基本上你需要做的是

There's a nice Styles and Themes guide on the android developers site. Basically what you need to do is

  1. 定义样式(或继承内置样式一).定义样式
  1. Define a style (or inherit a built-in one). To define a style

将XML文件保存在项目的 res/values/目录中.这XML文件的名称是任意的,但必须使用 .xml 扩展名并保存在 res/values/文件夹中.

save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.

XML文件的根节点必须是< resources> .

The root node of the XML file must be <resources>.

对于要创建的每种样式,在文件中添加一个元素带有唯一标识样式的名称(此属性为必填).

For each style you want to create, add a element to the file with a name that uniquely identifies the style (this attribute is required).

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.MyGreenTheme" parent="Theme.Light">
        <item name="android:windowBackground">#11aa22</item>
    </style>
</resources>

将资源文件命名为 themes.xml 很有用,因此更容易识别这些样式的用途.

It's useful to name the resource file themes.xml so it's easier to recognize what those styles are used for.

  1. 定义的样式应用于活动或查看您想要的风格化.您可以

  1. Apply the defined style to the activity or view that you want stylized. You can either

  • 在清单文件中设置活动/应用程序"主题:

<活动android:theme ="@ style/Theme.MyGreenTheme"/>

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆