如何创建自定义主题并在Android应用程序中使用它?

15
如何创建自定义主题并在代码中使用?
在菜单中如何实现主题选项并将其应用于活动?

请查看我的帖子:https://dev59.com/PHE85IYBdhLWcg3wyGp_ - Praveen
看一下这篇文章。也许这就是你要找的东西。 - orchidrudra
3个回答

14

在安卓开发者网站上有一份很棒的“样式和主题指南”。基本上你需要做的是:

  1. 定义一个样式(或继承一个内置的)。要定义一个样式,

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

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

对于每个要创建的样式,在文件中添加一个<style>元素,其中元素的名称唯一标识样式(此属性是必需的)。

例如:

<?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

  1. 应用定义好的样式到需要设置样式的活动或视图中,你可以:

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

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

    • 或者动态设置 - 使用Activity类对应的setter方法setTheme()

3
这个网站非常完美,可以创建所有必需的文件来制作自定义用户界面。前几周我亲自使用过它,对我非常有帮助。我与这个网站没有任何关联,但我觉得它非常有趣。希望这可以帮到你。 :)

0
创建自定义视图: public class CustomTextView extends AppCompatTextView {
public CustomTextView(Context context) {
    super(context);
    setCommonChanges(DefaultTheme.getInstance().textColor, true, context);
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setDefaultValues(context, attrs);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setDefaultValues(context, attrs);
}

private void setDefaultValues(Context context, AttributeSet attrs) {

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
    final int N = a.getIndexCount();
    int color = DefaultTheme.getInstance().textColor;
    boolean isCustomFont = a.getBoolean(R.styleable.CustomTextView_isCustomFont, true);
    for (int i = 0; i < N; ++i) {

        int colorIndex = a.getInteger(R.styleable.CustomTextView_tvBackground, 2);
        switch (colorIndex) {
            case 1:
                color = DefaultTheme.getInstance().headingTextColor;
                break;

            case 2:
                color = DefaultTheme.getInstance().textColor;
                break;

            case 3:
                color = DefaultTheme.getInstance().textHintColor;
                break;

            case 4:
                color = DesignUtils.getColorIdFromHexCode("#FFFFFF");
                break;

            case 5:
                color = DefaultTheme.getInstance().iconColor;
                break;
            case 6:
                color = DefaultTheme.getInstance().menuHeaderTextColor;
                break;
            case 7:
                color = DefaultTheme.getInstance().menuTextColor;
                break;
            case 8:
                color = DefaultTheme.getInstance().keyboardtextcolor;
                break;
            case 9:
                color = DesignUtils.getColorIdFromHexCode("#BEBEBE");
                break;
        }


    }
    a.recycle();

    setCommonChanges(color, isCustomFont, context);
}

private void setCommonChanges(int color, boolean isCustomFont, Context context) {
    if (isCustomFont) {
        Typeface typeface = DefaultTheme.getInstance().getTVFont(context);
        setTypeface(typeface, getTypeface().getStyle());
    }
    setTextColor(color);
}

public void updateTypeFace(int style){
    Typeface typeface = DefaultTheme.getInstance().getTVFont(getContext());
    setTypeface(typeface, style);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接