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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3758942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:40:30  来源:igfitidea点击:

How to create a custom theme and use it in an Android application?

androidthemes

提问by user428231

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

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

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

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

回答by stan0

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

android 开发人员站点上有一个很好的样式和主题指南。基本上你需要做的是

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

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 .xmlextension and be saved in the res/values/folder.

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).

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

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

对于您要创建的每个样式,向文件中添加一个元素,其名称可以唯一标识该样式(此属性是必需的)。

i.e.

IE

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

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

命名资源文件很有用,themes.xml这样可以更容易地识别这些样式的用途。

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

    • set the Activity/Application theme in the manifest file:

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

    • or set it dynamically - use the corresponding setter of the Activity class - setTheme().
  1. 定义的样式应用到您想要样式化的活动或视图。你可以

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

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

    • 或者动态设置它 - 使用 Activity 类的相应设置器 - setTheme()

回答by Michele La Ferla

Thisis perfect site which creates all the necessary files you need to make a custom UI. I used it personally a couple of weeks ago and it worked great for me.

是一个完美的网站,它创建了制作自定义 UI 所需的所有必要文件。几周前我亲自使用了它,它对我很有用。

I have no affiliation with this site but found it very interesting. Hope this may help you :)

我与这个网站没有从属关系,但发现它非常有趣。希望这可以帮助你:)

回答by KKSINGLA

Create Custome Views:

创建客户视图:

public class CustomTextView extends AppCompatTextView {

公共类 CustomTextView 扩展 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);
}