【发布时间】:2020-03-13 23:04:25
【问题描述】:
我已经看到了几个与类似问题相关的主题,但我无法成功地将其应用到我的案例中。实际上,当用户使用 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
【问题讨论】:
-
如果您更清楚确切的问题是什么,并且如果您链接到您提到的其他主题,这个问题会吸引更好的答案。