gpt4 book ai didi

java - ContextThemeWrapper 在 android 中更改样式

转载 作者:行者123 更新时间:2023-12-05 00:05:12 26 4
gpt4 key购买 nike

我已经看到几个与类似问题相关的线程,但我无法成功地将它应用到我的案例中。实际上,当用户使用 ContextThemeWrapper 单击按钮时,我正在尝试更改按钮的样式。因此,最初按钮设置为我在 XML 文档中链接的某种样式,并且我包含此代码以尝试使用 ContextThemeWrapper 并根据下面第一个链接中的建议改变它的外观。问题(我认为)是我不知道如何将新创建的按钮链接到我在屏幕上看到的物理对象。欢迎任何建议!谢谢!

    public class MainActivity extends AppCompatActivity {

private Button TestButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestButton = findViewById(R.id.button);

TestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContextThemeWrapper newContext = new ContextThemeWrapper(getApplicationContext(), R.style.custom);

TestButton = new TextView(newContext);

}
});
}
}

以下是我要引用的链接: ContexThemeWrapper to change style General question about different contexts

最佳答案

您有几个选项可以动态更改按钮的样式。

  1. 手动更改每个属性:

    TestButton.setTextColor(Color.RED);
    TestButton.setTextSize(24);
    // etc.
  2. 创建一个新按钮并替换布局中的旧按钮:

    ContextThemeWrapper newContext = new ContextThemeWrapper(MainActivity.this, R.style.custom);
    NewTestButton = new NewTestButton(newContext);
    Container.removeView(TestButton)
    Container.addView(NewTestButton)

关于java - ContextThemeWrapper 在 android 中更改样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60678251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com