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

发布于 2024-09-24 10:35:27 字数 51 浏览 2 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姜生凉生 2024-10-01 10:35:27

Android 开发者网站上有一个很好的样式和主题指南。基本上你需要做的是

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

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

XML 文件的根节点必须是

对于要创建的每种样式,将一个元素添加到文件中
具有唯一标识样式的名称(该属性是
必填)。

即,

<?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 类相应的 setter - setTheme()

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

  1. Define a style (or inherit a built-in one). To define a style

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.

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

i.e.

<?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.xml so it's easier to recognize what those styles are used for.

  1. Apply the 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().
蓝梦月影 2024-10-01 10:35:27

This is 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.

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

时光无声 2024-10-01 10:35:27

创建自定义视图:

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);
}

Create Custome Views:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文