Android:动态插入视图会导致表单小部件颜色发生变化

时间:2014-07-11 09:07:15

标签: android view colors widget

在我的Android活动中,我尝试在现有视图中动态添加视图。

我使用以下代码执行此操作:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.newactivity_name, null);
View insertPoint = findViewById(R.id.aVerticalLinearLayout);
((ViewGroup) insertPoint).addView(v, 0, new  ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

一切似乎工作得很好,除了某种程度上颜色/主题搞砸了 使所有形式的小部件看起来都非常褪色。

我希望它看起来如何: How it is displayed in the xml graphic display

它看起来如何: how it appears on the emulator after being inflated(忽略文字:提示不同)

有什么想法吗?

或者你能告诉我如何通过手动使calenderView和DatePicker看起来正常 设置颜色?

1 个答案:

答案 0 :(得分:0)

您应该在充气视图时设置父级,以便从父视图中获取样式和主题。

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View insertPoint = findViewById(R.id.aVerticalLinearLayout);
View v = vi.inflate(R.layout.newactivity_name, insertPoint); <-----
((ViewGroup) insertPoint).addView(v, 0, new  ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));