如何在android主题声明中添加自定义项目? [英] How to add custom item in android Theme declaration?

查看:109
本文介绍了如何在android主题声明中添加自定义项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的styles.xml中没有几个自定义主题
现在,只要该活动采用主题,它就会使用 colorPrimary colorPrimaryDark colorAccent 值.
对于布局的背景,我使用的是?attr/colorAccent ,因此它可以根据所选主题选择背景颜色.
如果我使用以上任何一个值,都可以正常工作.但是我想为我的背景色定义一个自定义项目值.
我在下面尝试过这种方法,但是没有用.有什么想法可以使它起作用吗?
我的自定义主题具有自定义值:

I'm having few custom themes in my styles.xml
Now whenever the activity takes the theme, it uses the colorPrimary, colorPrimaryDark and colorAccent values.
For my layout's background I'm using ?attr/colorAccent, so it can pick the background color based on the selected theme.
If I use any of the above values it works fine. But I want to define a custom item value for my background color.
I tried like this below but it didn't worked. any ideas to make it work ?
My custom theme with custom value:

<style name = "customTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#4285f4</item>
    <item name="colorPrimaryDark">#2C75F2</item>
    <item name="colorAccent">#E1FFC7</item>
    <item name="customBgColor">#d3d3d3</item>
</style>


我想以


And I want to use it in layout's style as

<style name="layoutStyle" >
    <item name="android:background">?attr/customBgColor</item>
</style>

推荐答案

创建一个如图所示的attrs.xml文件.

Create a attrs.xml file shown in image.

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <!-- Other values-->
   <attr name="customBgColor" format="reference" />

</resources>

customTheme 1

<style name = "customTheme1" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Other values-->
    <item name="customBgColor">#d3d3d3</item>
</style>

customTheme 2

<style name = "customTheme2" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Other values-->
    <!-- Black Color in theme2-->
    <item name="customBgColor">#111111</item>
</style>

例如将颜色"设置为"TextView".

Setting Color to TextView as example.

您可以在任何位置的任何小部件中以类似方式使用它.

You can use it in similar way in any widget anywhere.

TextView用于以下活动.

<TextView
    android:id="@+id/txt_rate_us_about"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Rate us on Play Store!"
    android:textColor="?attr/customBgColor"
    android:textSize="20dp" />

想要动态设置主题.

public class AboutUsActivity extends Activity {

    int theme = 1;
    // int theme = 2;  2nd theme.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        switch (theme) {
            default:
            case 1:
                this.setTheme(R.style.customTheme1);
                break;
            case 2:
                this.setTheme(R.style.customTheme2);
                break;

        }
        // you must call `setTheme()` before `setContentView()`
        setContentView(R.layout.activity_about);

    }

对于多个活动,您分别为每个活动设置了主题.

For multiple activities you have set theme for each of them separately.

这篇关于如何在android主题声明中添加自定义项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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