当使用ApplicationContext的充气主题/样式不适用 [英] Theme/Style is not applied when inflater used with ApplicationContext

查看:176
本文介绍了当使用ApplicationContext的充气主题/样式不适用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主题,指定文字颜色为TextView的为红色。

我使用LayoutInflater实例TextView的。问题是,样式不应用到TextView中吹气时使用的ApplicationContext创建 - 颜色不红。一切工作正常时LayoutInflater使用活动创建。

为什么出现这种情况,如何能解决吗?

/res/values​​/styles.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=MyTheme的>
        <项目名称=机器人:textViewStyle> @风格/ MyTextView< /项目>
    < /风格>

    <样式名称=MyTextView父=@安卓风格/ Widget.TextView>
        <项目名称=机器人:文字颜色>#F00< /项目>
    < /风格>
< /资源>
 

AndroidManifest.xml中:

 <应用
    机器人:图标=@可绘制/图标
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ MyTheme的
    >
 

code:

 大众A级延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.test_a);

        最后LayoutInflater goodInflater = getInflater((活动)本);
        最终LayoutInflater badInflater = getInflater(getApplicationContext());
        最后的LinearLayout容器=(的LinearLayout)findViewById(R.id.container);

        findViewById(R.id.add_with_appContext).setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                加(集装箱,badInflater); //创建一个灰色的TextView
            }
        });

        findViewById(R.id.add_with_activity).setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                加(集装箱,goodInflater); //创建一个红色的TextView
            }
        });
    }

    私人LayoutInflater getInflater(上下文的背景下){
        返回(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    私人无效添加(的LinearLayout容器,LayoutInflater充气){
        inflater.inflate(R.layout.my_template,集装箱,真正的);
    }
}
 

/res/layout/test_a.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>

    <按钮
        机器人:文本=添加与AppContext
        机器人:ID =@ + ID / add_with_appContext
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        />

    <按钮
        机器人:文本=添加与活动
        机器人:ID =@ + ID / add_with_activity
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        />

    <的LinearLayout
        机器人:ID =@ + ID /容器
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        />

< / LinearLayout中>
 

/res/layout/my_template.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    >

    <的TextView
        机器人:ID =@ + ID /文
        机器人:文本=一些文本......
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
    />

< / LinearLayout中>
 

解决方案

解决方案#1

该充气方法接受可选的ViewGroup根的说法:

 公共景观的inflate(INT资源,ViewGroup中根,布尔attachToRoot)
 

如果我们的价值传递为'根'的参数,比因此我们可以用它来从那里我们可以得到正确的LayoutInflater得到'活动方面:

 的ViewGroup根>活动背景> LayoutInflater
 

所以,我的code可能是:

 私人无效添加(的LinearLayout容器){
    LayoutInflater充气= getInflater(container.getContext());
    inflater.inflate(R.layout.my_template,集装箱,真正的);
}
 

解决方案#2

只是试图以编程方式设置应用上下文的主题,它的

  getApplicationContext()setTheme(R.style.MyTheme)。
 

我认为这是合乎逻辑的期望此标记:

 <应用
    机器人:图标=@可绘制/图标
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ MyTheme的
    >
 

自动对其进行设置,但事实并非如此。

I have theme that specifies textColor for TextView as red.

I am using LayoutInflater to instantiate TextView. The problem is that styles are not applied to TextView when inflater created using ApplicationContext - the color is not red. All works fine when LayoutInflater created using activity.

Why this happens, and how can be fixed?

/res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme">
        <item name="android:textViewStyle">@style/MyTextView</item>
    </style>

    <style name="MyTextView" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#f00</item>
    </style>
</resources>

AndroidManifest.xml:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    >

Code:

public class A extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_a);

        final LayoutInflater goodInflater = getInflater((Activity)this); 
        final LayoutInflater badInflater = getInflater(getApplicationContext());
        final LinearLayout container = (LinearLayout)findViewById(R.id.container);

        findViewById(R.id.add_with_appContext).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add(container, badInflater); // Creates gray TextView
            }            
        });

        findViewById(R.id.add_with_activity).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add(container, goodInflater); // Creates red TextView
            }            
        });
    }

    private LayoutInflater getInflater(Context context) {
        return (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private void add(LinearLayout container, LayoutInflater inflater) {
        inflater.inflate(R.layout.my_template, container, true);
    }
}

/res/layout/test_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button 
        android:text="Add with AppContext" 
        android:id="@+id/add_with_appContext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <Button 
        android:text="Add with Activity" 
        android:id="@+id/add_with_activity" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"  
        />

</LinearLayout>

/res/layout/my_template.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/text"
        android:text="Some text..."
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
    />

</LinearLayout>

解决方案

Solution # 1

The inflate method accepts optional 'ViewGroup root' argument:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

If we have value to pass as 'root' parameter, than hence we can use it to get 'activity context' from where we can get correct LayoutInflater:

ViewGroup root > activity context > LayoutInflater

So my code could be:

private void add(LinearLayout container) {
    LayoutInflater inflater = getInflater(container.getContext());
    inflater.inflate(R.layout.my_template, container, true);
}

Solution # 2

Just tried to set Application Context theme programmatically, and it works:

getApplicationContext().setTheme(R.style.MyTheme);

I think it was logical to expect this markup:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    >

to set it automatically, but it does not.

这篇关于当使用ApplicationContext的充气主题/样式不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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