我一直在尝试为我的Views
整个应用程序找到奇怪的格式样式不一致的原因,我想我已经用这个例子缩小了范围。
Views
我使用完全相同的程序设置了两个等效的各种 s 布局,并且只改变了Context
创建时提供的内容。在第一组中,每个View
都是通过应用程序的上下文创建的Activity.getApplicationContext()
,而在第二组中,View
s 是通过 为活动的上下文提供的this
。
结果大不相同:
关于为什么使用应用程序上下文会导致屏幕截图中出现垃圾格式(并且不一致 - 颜色是白色和灰色)的任何建议?
活动代码:
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);
}
}
显现:
<?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>