我希望能够在运行时覆盖我的应用程序资源,例如R.colour.brand_colour或R.drawable.ic_action_start.我的应用程序连接到CMS系统,该系统将提供品牌颜色和图像.一旦应用程序下载了CMS数据,它就需要能够重新进行自我修饰.
我知道你要说什么 - 在运行时覆盖资源是不可能的.
除了它有点.特别是我从2012年开始发现了这个学士论文,它解释了基本概念 - android扩展中的Activity类ContextWrapper,它包含了attachBaseContext方法.您可以覆盖attachBaseContext以使用您自己的自定义类包装Context,该自定义类将覆盖getColor和getDrawable等方法.你自己的getColor实现可以看起来像它想要的颜色.该书法库使用类似的方法注入定制LayoutInflator可以处理负荷自定义字体.
我创建了一个简单的Activity,它使用这种方法来覆盖颜色的加载.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CmsThemeContextWrapper(newBase));
}
private class CmsThemeContextWrapper extends ContextWrapper{
private Resources resources;
public CmsThemeContextWrapper(Context base) {
super(base);
resources = new Resources(base.getAssets(), base.getResources().getDisplayMetrics(), base.getResources().getConfiguration()){
@Override
public void getValue(int id, TypedValue outValue, boolean resolveRefs) throws NotFoundException {
Log.i("ThemeTest", "Getting value for resource " + getResourceName(id));
super.getValue(id, …Run Code Online (Sandbox Code Playgroud) 我试图通过样式扩大我的所有应用程序对话框按钮上的文本大小.以下代码将更改按钮背景颜色甚至文本大小写,但由于某种原因,textSize项目不符合:
<style name="MyAppTheme" parent="@android:style/Theme.Holo">
<item name="android:dialogTheme">@style/MyApp.Dialog</item>
<item name="android:alertDialogTheme">@style/MyApp.Dialog.Alert</item>
</style>
<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
<item name="android:borderlessButtonStyle">@style/MyApp.BorderlessButton</item>
</style>
<style name="MyApp.Dialog.Alert" parent="@style/MyApp.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="MyApp.BorderlessButton" parent="@android:style/Widget.Holo.Button.Borderless">
<item name="android:textSize">50sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:background">#800</item>
</style>
Run Code Online (Sandbox Code Playgroud)

为什么没有读取textSize?我需要做些什么来扩大它?
android android-layout android-theme android-dialog android-styles
我们正在开发一种用于工业用途的Android应用程序.我们增加了操作栏,文本等的大小,以帮助提高可见性和触摸目标大小.一切都运作良好,但我无法增加操作栏溢出菜单的列表项的高度:

四处挖掘我已经能够使用改变弹出菜单的背景颜色
<style name="MyTheme" parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/MyPopupMenu</item>
</style>
<style name="MyPopupMenu" parent="android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">#800</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是我似乎无法找到更改菜单项本身的高度.关于在操作栏中自定义微调器的堆栈溢出有很多帖子,但关于操作溢出菜单很少.