【问题标题】:How can I change color of specific layout component?如何更改特定布局组件的颜色?
【发布时间】:2020-01-25 01:59:30
【问题描述】:

如何更改layout 中星形组件的颜色? 可以在代码下面完成还是我必须在 xml 中工作?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.Custom);
    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"));
    layout.setBackgroundColor(0xffff0000);
    setContentView(layout);    }

【问题讨论】:

标签: android


【解决方案1】:

你应该有你的布局组件的参考:

private ProgressBar progressBar;
private RadioButton radioButton;
private RatingButton ratingButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.Custom);
    ViewGroup layout = getLayout(getIntent().getStringExtra("key1"));
    if (layout == null) return;

    progressBar = new ProgressBar(this);
    radioButton = new RadioButton(this);
    ratingButton = new RatingButton(this);

    layout.addView(progressBar);
    layout.addView(radioButton);
    layout.addView(ratingBar);

    progressBar.setBackgroundColor(...);
    radioButton.setBackgroundColor(...);
    ratingButton.setBackgroundColor(...);   
    //And so on, with the appropriate method for each View

    layout.setBackgroundColor(Color.parseColor("#3333ff"));
    layout.setBackgroundColor(0xffff0000);
    setContentView(layout);   
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2018-08-22
    • 2017-05-03
    • 2018-02-13
    • 2018-09-15
    • 2023-04-01
    • 2018-05-16
    相关资源
    最近更新 更多