【问题标题】:How to set Custom Theme for layout generated in code? (viewgroup)如何为代码中生成的布局设置自定义主题? (视图组)
【发布时间】:2020-01-22 05:31:31
【问题描述】:

对于我的主要活动,以下代码将主题设置为我的自定义主题:

    setTheme(R.style.Custom);

但是我从中得到的布局呢:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup layout = getLayout(getIntent().getStringExtra("key1"));
    if (layout == null) return;
    layout.addView(new ProgressBar(this));
    layout.addView(new RadioButton(this));
    layout.addView(new RatingBar(this));
    layout.addView(new CheckBox(this));
    layout.addView(new Switch(this));
    layout.addView(new SeekBar(this));
    layout.setBackgroundColor(Color.parseColor("#3333ff"));
    setContentView(layout);    }

我想改变 ViewGroup 布局的主题,我试过了

    layout.setTheme(R.style.Custom); but it didn't work, what's the correct function to do this

这是我的主题代码:(是否可以更改星星的颜色,条 等等在这里?)

    <style name="Custom" parent="Theme.AppCompat.DayNight">
    <item name="colorPrimary">#99ff99</item>
    <item name="colorPrimaryDark">#009933</item>
    <item name="colorAccent">#f542e3</item>
</style>

【问题讨论】:

    标签: android


    【解决方案1】:

    ViewGroup 没有“主题”或类似的属性。 布局仅用于定位您的小部件,仅此而已

    因此,您应该在 onCreate() 中的所有其他内容之前调用 setTheme(),就像这样:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Custom);
    }
    

    或者您可以通过将属性传递给每个构造函数How to set style to a progressBar programmatically on Android

    ,为每个小部件(ProgressBar、RadioButton 等)设置自定义样式

    【讨论】:

      猜你喜欢
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多