我正在制作一个 Android 应用程序并正在考虑主题。
如果我没有为我的 Android 应用程序声明主题,将使用什么主题?
我在哪里可以找到这些信息?
使用其中之一和其他的标准是什么?
我在想,如果我想自定义我的应用程序,我必须扩展一个主题并自定义我想要的项目。如果它假设其中之一为默认值怎么办?我怎么知道默认值是什么?
默认主题根据 API 级别的不同而有所不同(与通用 UI 保持一致)。
在 API < 10, the theme was a set of styles (as in the link below) known as
Theme
上,在 API 10 之上,默认主题为 Theme_Holo
,现在,从 API 21 开始,默认主题已变为 Theme.Material
。
Theme
或 Theme.AppCompat
Holo_Theme
或 Theme.AppCompat
Theme.Material
大多数样式都可以通过
android.support
库获得。
PS: AFAIK 浅色主题一直是默认主题。
最好自己定义一个默认主题,而不是依赖android来选择默认主题。这是因为不同版本的 android 可能有完全不同的默认主题,并且可能会弄乱你的布局。
您可以在
AndroidManifest.xml
中为您的应用程序声明一个主题
<application android:theme="@style/MyTheme" .....>
然后在
res/values
文件夹中,编辑/添加文件 themes.xml
并添加如下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="@android:style/Theme.Holo">
... customize your theme here
</style>
</resources>
您可以将主题的
parent
编辑为您想要的任何内容...
如果您根本不需要
任何自定义,也可以直接在
@android:style/Theme.Holo
中使用AndroidManifest.xml
。
如果 API 版本低于 11,请使用
Theme.AppCompat.Holo
App的默认主题是在Resources.java中实现的!
/**
* Returns the most appropriate default theme for the specified target SDK version.
* <ul>
* <li>Below API 11: Gingerbread
* <li>APIs 11 thru 14: Holo
* <li>APIs 14 thru XX: Device default dark
* <li>API XX and above: Device default light with dark action bar
* </ul>
*
* @param curTheme The current theme, or 0 if not specified.
* @param targetSdkVersion The target SDK version.
* @return A theme resource identifier
* @hide
*/
public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
return selectSystemTheme(curTheme, targetSdkVersion,
com.android.internal.R.style.Theme,
com.android.internal.R.style.Theme_Holo,
com.android.internal.R.style.Theme_DeviceDefault,
com.android.internal.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}
/** @hide */
public static int selectSystemTheme(int curTheme, int targetSdkVersion, int orig, int holo,
int dark, int deviceDefault) {
if (curTheme != 0) {
return curTheme;
}
if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) {
return orig;
}
if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return holo;
}
if (targetSdkVersion < Build.VERSION_CODES.CUR_DEVELOPMENT) {
return dark;
}
return deviceDefault;
}
根据API级别不同,所以你最好在AndroidManifest.xml中定义自己的AppTheme,以保证所有API级别设备上的Theme。
请参考之前的答案。