根据所选范围内的Andr​​oid全息光造型变化 [英] Android Holo Light styling changes depending on chosen context

查看:245
本文介绍了根据所选范围内的Andr​​oid全息光造型变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图寻找古怪的格式样式不一致的原因我的浏览在我的应用程序,我想我已经把范围缩小这个例子。

我设置各种浏览 s的完全相同的程序和改变上下文两个相等的布局创作提供。在第一组中,每个查看与应用程序的情况下,通过 Activity.getApplicationContext创建(),而在第二设置查看,则通过喂食活动的背景下,这个

其结果是截然不同的:

为什么使用应用程序上下文任何建议引起了垃圾(和不一致 - 颜色有白色和灰色)?格式化看到的截图

活动code:

 进口android.os.Bundle;
进口android.app.Activity;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;
进口android.widget.Spinner;
进口android.widget.TextView;

公共类MainActivity延伸活动{

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

        // TextViews
        TextView的TV1 =新的TextView(getApplicationContext());
        tv1.setText(随着应用程序上下文);
        TextView的TV2 =新的TextView(本);
        tv2.setText(随着活动范围内);

        //纱厂
        微调SP1 =新的微调(getApplicationContext());
        sp1.setAdapter(新ArrayAdapter<字符串>(getApplicationContext(),android.R.layout.simple_spinner_item,新的String [] {应用程序上下文1,应用程序上下文2,应用程序上下文3}));
        微调SP2 =新的微调(本);
        sp2.setAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_spinner_item,新的String [] {法案方面1,环境法2,环境法3}));

        // Edittexts
        EditText上ET1 =新的EditText(getApplicationContext());
        et1.setText(应用文字);
        ET2的EditText =新的EditText(本);
        et2.setText(活动上下文);

        //按钮
        按钮B1 =新按钮(getApplicationContext());
        b1.setText(应用文字);
        按钮B2 =新的按钮(这一点);
        b2.setText(活动上下文);

        //布局结构
        的LinearLayout LL =新的LinearLayout(本);
        ll.setOrientation(LinearLayout.VERTICAL);

        ll.addView(TV1);
        ll.addView(SP1);
        ll.addView(ET1);
        ll.addView(B1);
        ll.addView(TV2);
        ll.addView(SP2);
        ll.addView(ET2);
        ll.addView(B2);

        的setContentView(Ⅱ);
    }

}
 

清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.test.test
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =11
        机器人:targetSdkVersion =17/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@安卓风格/ Theme.Holo.Light>
        <活动
            机器人:名称=com.test.test.MainActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.Holo.Light>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

解决方案

在Android的源 getApplicationContext()返回延伸 ContextWrapper ,因此,使用 getApplicationContext()你逝去的 ContextWrapper 的子类,但是,当你通过,你是传递一个活动对象延伸 ContextThemeWrapper ,所以你通过 ContextThemeWrapper 子类。现在的 ContextWrapper ContextThemeWrapper ContextWrapper <区别A HREF =htt​​p://developer.android.com/reference/android/content/ContextWrapper.html相对=nofollow>只是代表所有的来电到另一个语境和 ContextThemeWrapper 允许你修改的是什么,在主题包装方面

虽然,这个问题是更多关于到底为什么发生这种情况(而不是它是明确的原因),这里也有一些有用的帖子这解释了使用应用程序上下文,以及如何正确正确选择方面的危险:

从@CommonsWare最重要的是: getApplicationContext()不是一个完整的上下文,因此不支持一切活动呢。

有关背景下,应该澄清一切真棒帖子:

I have been trying to find the cause of weird formatting style inconsistencies for my Views throughout my application and I think I have narrowed it down with this example.

I set up two equivalent layouts of various Viewss with exactly the same procedure and only varying the Context supplied in creation. In the first set, each View is created with the application's context through Activity.getApplicationContext(), whereas in the second set the Views are fed the activity's context through this.

The result is vastly different:

Any suggestions on why using the application context causes the garbage (and inconsistent - colours are white as well as grey) formatting seen in the screenshot?

Activity code:

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TextViews
        TextView tv1 = new TextView(getApplicationContext());
        tv1.setText("With Application  context");
        TextView tv2 = new TextView(this);
        tv2.setText("With Activity  context");      

        // Spinners
        Spinner sp1 = new Spinner(getApplicationContext());
        sp1.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, new String[] {"App context 1", "App context 2", "App context 3"}));
        Spinner sp2 = new Spinner(this);
        sp2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {"Act context 1", "Act context 2", "Act context 3"}));

        // Edittexts
        EditText et1 = new EditText(getApplicationContext());
        et1.setText("Application Context");
        EditText et2 = new EditText(this);
        et2.setText("Activity Context");        

        // Buttons
        Button b1 = new Button(getApplicationContext());
        b1.setText("Application Context");
        Button b2 = new Button(this);
        b2.setText("Activity Context");     

        // Layout structure
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);

        ll.addView(tv1);
        ll.addView(sp1);
        ll.addView(et1);
        ll.addView(b1);
        ll.addView(tv2);
        ll.addView(sp2);
        ll.addView(et2);
        ll.addView(b2);

        setContentView(ll);
    }

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light" >
        <activity
            android:name="com.test.test.MainActivity"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.Holo.Light" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

解决方案

In Android sources getApplicationContext() returns Application object which extends ContextWrapper, so using getApplicationContext() you are passing ContextWrapper subclass, but when you pass this, you are passing an Activity object which extends ContextThemeWrapper so you are passing ContextThemeWrapper subclass. Now the difference between ContextWrapper and ContextThemeWrapper is that ContextWrapper simply delegates all of its calls to another Context and ContextThemeWrapper allows you to modify the theme from what is in the wrapped context.

Although, the question was more about why exactly this is happening (as opposed to the cause which was clear), here are also some helpful posts which explain the perils of incorrectly using application context and how to choose a context correctly:

Most importantly from @CommonsWare: "getApplicationContext() is not a complete Context and consequently does not support everything that Activity does."

Awesome post about Context that should clarify everything:

这篇关于根据所选范围内的Andr​​oid全息光造型变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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