【问题标题】:Android: Dynamically inserting view results in change in colors of form widgetsAndroid:动态插入视图会导致表单小部件的颜色发生变化
【发布时间】:2014-07-11 09:07:14
【问题描述】:

在我的 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看起来正常吗 设置颜色?

【问题讨论】:

    标签: android view colors widget


    【解决方案1】:

    你应该在膨胀视图时设置父视图,以便它从父视图获取样式和主题。

    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));
    

    【讨论】:

    • 最终解决问题的原因是我目前不完全理解,只是简单地使用 View v = getLayoutInflater().inflate(R.layout.activity_reminder, insertPoint,false);而不是将 LayoutInflater 作为自己的变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多